### Thread: splitting a std::string into vectors

1. No Profile Picture
njd
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
5
Rep Power
0

#### splitting a std::string into vectors

hi

how would i split a std::string into vectors?

say i had "one two :three four five"
how would i make it so everything after the "two :" was in the very last vector and the things before that were in seperate vectors delimited by ' '?

so:

"one two :three four five" would end up as "one", "two", ":three four five"

thanks
2. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
18
Here is one way--it involves putting a string object in a stream, and then reading from it just like you do with cin>>,
Code:
```#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
const int size = 5;
string str="one two :three four five";

//Put the string in the stream inString:
istringstream inString(str);

//Vector to hold the words:
vector<string> pieces(size);

for(int i=0; i<size; i++)
{
//Read from the stream using the operator>>,
//which works just like cin>>:
inString>>pieces[i];
}

//Vector to hold the two phrases:
vector<string> joined(2);

joined[0]= pieces[0] + " " + pieces[1];
joined[1]= pieces[2] + " " + pieces[3] + " " + pieces[4];

cout<<joined[0]<<endl;
cout<<joined[1]<<endl;

return 0;
}```
Or, the more complicated way with string functions:
Code:
```#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
string text="one two :three four five";
int start = 0;
int end = 0;
vector<string> pieces(5);

//Find the first non space chacter in
//the string:
start = text.find_first_not_of(" ");

int i = 0;
while(start != string::npos)
{
//Find the index position of the next space:
end = text.find_first_of(" ", start + 1);

//If no spaces are found in the string
//set end at the end of the string:
if(end == string::npos)
end = text.length();

pieces[i]= text.substr(start, end - start);

//Set start to equal the next non space
//character:
start = text.find_first_not_of(" ", end + 1);

//Increment the vector index:
i++;
}

vector<string> joined(2);
joined[0] = pieces[0] + " " + pieces[1];
joined[1] = pieces[2] + " " + pieces[3] + " " + pieces[4];

cout<<joined[0]<<endl;
cout<<joined[1]<<endl;

return 0;
}```
Last edited by 7stud; August 8th, 2003 at 05:38 AM.
3. No Profile Picture
njd
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
5
Rep Power
0
hi, that works good.

but is there a way so when the string gets to the ":three" part, everything (including that) after that part of the string goes into one vector? because the string will have different lengths after the ':'

hope you understand
thanks
4. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
18
I'm not sure how you could do that with the first method, but you could use the string function find_first_of() to look for the ":" and everything from index position 0 to the index position of the colon - 1 would be the first string, and everything from the ":" to end of the string (=text.length()) would be your second string.
Last edited by 7stud; August 8th, 2003 at 05:51 AM.
5. No Profile Picture
njd
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
5
Rep Power
0
PHP Code:
```     vector<string> sv(4);     int s = 0;          s = tp.find_first_of(":", 1);     sv[3] = tp.substr(s + 1);     istringstream instr(tp.substr(0, s - 1));          for (int i = 0; i < 3; i++) {         instr >> sv[i];     }  ```
i did it like that and it works, thanks!
it probably could be done better, but oh well :)

thanks again
6. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
18
You can use getline() with the first method to split the string in two pieces at the colon:
Code:
```#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main()
{
const int size = 2;
string str="one two :three four five";

//Put the string in the stream inString:
istringstream inString(str);

//Vector to hold the pieces
vector<string> pieces(size);

getline(inString, pieces[0], ':');
getline(inString, pieces[1]);

//Since getline() removes the delimiter colon,
//insert the colon back into the front of the
//second string:
pieces[1].insert(0, ":");

cout<<pieces[0]<<endl;
cout<<pieces[1]<<endl;

return 0;
}```
7. No Profile Picture
Contributing User
Devshed Beginner (1000 - 1499 posts)

Join Date
Feb 2001
Posts
1,481
Rep Power
18
Well, you combined the two methods--good for you. I just re-read what you were trying to do, and I realized you wanted to split the first part up at the spaces and everything after the colon was to be one string.