Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 8th, 2003, 01:19 AM
 njd
Junior Member

Join Date: Aug 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation 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
August 8th, 2003, 04:59 AM
 7stud
Contributing User

Join Date: Feb 2001
Posts: 1,365
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
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
August 8th, 2003, 05:33 AM
 njd
Junior Member

Join Date: Aug 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation 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
August 8th, 2003, 05:48 AM
 7stud
Contributing User

Join Date: Feb 2001
Posts: 1,365
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
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
August 8th, 2003, 06:01 AM
 njd
Junior Member

Join Date: Aug 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation 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
August 8th, 2003, 06:06 AM
 7stud
Contributing User

Join Date: Feb 2001
Posts: 1,365
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
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
August 8th, 2003, 06:13 AM
 7stud
Contributing User

Join Date: Feb 2001
Posts: 1,365
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > splitting a std::string into vectors