String-Array bei Semikolon trennen



  • Hallo zusammen,

    ich bin änfanger in C++ und würde gerne den Inhalt eines Strings bei z.B. einem Semikolon trennen!

    Also...:

    string str("Stuhl;Milch;Salat;Honig");

    jetzt soll immer wenn das Trenn-Zeichen ";" kommt => iZaehler++!

    Heißt ich möchte herraus finden wieviel (in meinen fall) Gegenstände mein String enthält.

    Ich hab schon viel gegoogelt, allerdings verstehe ich von den Usern geschriebenen Hilfe dazu nicht oder sind auf Englisch geschrieben. (Bin leider kein AS in Englisch :P)

    Wäre super wenn mir jemand helfen könnte.



  • Zwei mögliche Lösungen.
    Simon

    #include <string>
    #include <iostream>
    #include <algorithm>  // nur für die 1. Lsg nötig
    
    int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
    {
    	std::string str("Stuhl;Milch;Salat;Honig");
    
    	// Lösung 1.
    	//std::string::size_type c = std::count(str.begin(), str.end(), ';');
    	//if ( ! str.empty())
    	//{
    	//	++c;
    	//}
    
    	// Lösung 2.
    	long c = 0;
    	std::string::const_iterator it = str.begin();
    	std::string::const_iterator end = str.end();
    	if (it != end)	// Ist str leer?
    	{
    		++c;		// str ist nicht leer und enthält desshalb min. 1 Element.
    		for (; it != end; ++it)	// zähle jetzt die Elemente
    		{
    			if (';' == *it)
    			{
    				++c;
    			}
    		}
    	}
    
    	std::cout << "Es sind " << c << " Dinge enthalten." << std::endl;
    }
    


  • Hi theta,

    sorry. Das ganze funktioniert wunderbar allerdings sehe ich das du nicht

    using namespace std; nutzt. Hat das damit was zutun das dein Quellcode anders aussieht?

    Weil leider versteh ich das nicht so sehr...



  • using namespace std; schreibt im prinzip nur std:: vor die Funktionen.



  • Also ich habs jetzt mal versucht umzuschreiben.. allerdings kein erfolgt nur ERRORs 😞

    #include <string>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void main()
    {
        string str = ("Stuhl;Milch;Salat;Honig");
        string start;
    	string ende;
    
        // Lösung 2.
        long c = 0;
        start = str.begin();
        ende = str.end();
        if (start != ende)    // Ist str leer?
        {
            c++;        // str ist nicht leer und enthält desshalb min. 1 Element.
            for (; start != ende; start++)    // zähle jetzt die Elemente
            {
                if (';' == start)
                {
                    c++;
                }
            }
        }
    
        cout << "Es sind " << c << " Dinge enthalten." << endl;
    }
    


  • sinus9999 schrieb:

    allerdings kein erfolgt nur ERRORs 😞

    Wie wär's, wenn du uns die auch mal nennst? 😉



  • Ups 😃

    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_String_iterator<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion)

    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_String_iterator<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion)

    error C2676: binary '++' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

    error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'char'

    error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'char'

    error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'char'

    error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'char'

    error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'char'



  • start und ende sollten natürlich

    string::iterator
    

    sein und

    if (';' == start)
    

    muss wohl

    if (';' == [b]*[/b]start)
    

    lauten



  • CStringArray arr;
    CString Data ="Das;ist;ein;Test";
    char * delim = ";";
    int old = 0,pos = 0;
    
    while(-1 != (pos = Data.Find(delim,pos)))
    {
    	arr.Add(Data.Mid(old,pos - old));
    	old = ++pos;
    }
    if(old < Data.GetLength())
    	arr.Add(Data.Mid(old,Data.GetLength() - old));
    


  • noch ne Version:

    CStringArray arr;
    CAtlString str(_T("Das;ist;ein;Test"));
    CAtlString resToken;
    int curPos = 0;
    
    resToken= str.Tokenize(_T(";"),curPos);
    while (resToken != _T(""))
    {
       arr.Add(resToken);
       resToken = str.Tokenize(_T(";"), curPos);
    };
    

Anmelden zum Antworten