-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringToVector.cpp
44 lines (34 loc) · 989 Bytes
/
stringToVector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
// Function Prototypes
using namespace std;
vector<string> StringToVector(string theString,
char separator);
int main() {
// ----- 1. CONVERT STRING TO VECTOR -----
// Create a function that receives a string and separator
// and returns a string vector
string sSentence = "This is a random string";
// Create a vector
vector<string> vec = StringToVector(sSentence, ' ');
// Cycle through each index in the vector and print
// out each word
for(int i = 0; i < vec.size(); ++i){
cout << vec[i] << "\n";
}
return 0;
}
vector<string> StringToVector(string theString,
char separator){
vector<string> vecWords;
stringstream ss(theString);
string sIndivStr;
while(getline(ss, sIndivStr, separator)){
vecWords.push_back(sIndivStr);
}
return vecWords;
}