char*[] split( char* str , char* cut )

Status
Niet open voor verdere reacties.

blua tigro

Gebruiker
Lid geworden
21 apr 2009
Berichten
48
ik zoek n split functie
die n 'zin' in losse worden kan knippen
bv char* zin = "dit is een test" ;
naar
char* woord[]={ "dit" , "is" , "een" , "test" } ;
de 'zinnen' hebben geen vaste lengte
vector<char*> split( , ) mag ook
dan heb ik ook vector::connt
cxio diversas el tio rezultas cxio samvaloras
 
dan heb ik ook vector::connt
cxio diversas el tio rezultas cxio samvaloras

Wtf?

In ieder geval:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main(void)
{
    char* zin = "dit is een test" ;
    vector<string> losse_woorden;
    string woord;
    stringstream ss(zin);
    while (getline(ss,woord, ' '))
        losse_woorden.push_back(woord);
    
    // even laten zien dat het werkt:
    for (unsigned i = 0; i < losse_woorden.size(); i++)
        cout << i << ": " << losse_woorden.at(i) << "\n";

    return 0;
}

output:
Code:
0: dit
1: is
2: een
3: test

Mocht je iets niet snappen aan het bovenstaande, dan kun je het altijd vragen. Hier alvast een link:
stringstream
 
resultaat

dan wordt het dus :
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std ;

vector<char*>split( char* str , char* cut )
{
   vector<char*> out ;
   string word ;
   stringstream ss( str ) ;
   while getline( ss , word , cut ) ;
   {
      out.push_back( word ) ;
   }
   return out ;
}
ik vroeg me alleen nog af
werkt het ook met string ipv char*
[ dat zou ik netter vinden ]
-------------------------------------------------------------------------------------------------------------------------------
cxio diversas el tio rezultas cxio samvaloras
 
resultaat 2

ja en nee

we hebben nu

vector<char*> split( char* str , char* cut )
en
vector<string> split( string str , string cut )

dacht ik

of moet cut een char zijn ?

----------------------------------------------------------------------------------------------------------------------------------
cxio diversas el tio rezultas cxio samvaloras
 
Ik zou inderdaad
- van 'str' een string maken
- vector<char*> out veranderen in vector<string> out;
- char* cut veranderen in char cut
- en dus de functie aanpassen naar:
Code:
vector<string>split( string str , char cut )
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan