Can anyone tell me who sizeof &tokens only return a int of 4 no matter
how many
strings that are in the token[].
thanks,
Grant
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
// Skip delimiters at beginning.
string::size_ty pe lastPos = str.find_first_ not_of(delimite rs, 0);
// Find first "non-delimiter".
string::size_ty pe pos = str.find_first_ of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_bac k(str.substr(la stPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_ not_of(delimite rs, pos);
// Find next "non-delimiter"
pos = str.find_first_ of(delimiters, lastPos);
}
}
int main()
{
vector<string> tokens;
string str(";1,2,3,4,5 ,6,7,8,9,0"); // replace with getline
string start = str.substr(0,1) ;
if(start == ";")cout<<"HELL O WORLD"<<'\n'; // skip this line for
comments
int len = str.length();
for (int a=0; a<len; ++a)
str[a]=toupper(str[a]);
Tokenize(str, tokens);
for (int i=0;i<sizeof(&t okens);++i){ // check amount of tokens
cout << tokens[i]<<'\n';
}
}
how many
strings that are in the token[].
thanks,
Grant
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
// Skip delimiters at beginning.
string::size_ty pe lastPos = str.find_first_ not_of(delimite rs, 0);
// Find first "non-delimiter".
string::size_ty pe pos = str.find_first_ of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_bac k(str.substr(la stPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_ not_of(delimite rs, pos);
// Find next "non-delimiter"
pos = str.find_first_ of(delimiters, lastPos);
}
}
int main()
{
vector<string> tokens;
string str(";1,2,3,4,5 ,6,7,8,9,0"); // replace with getline
string start = str.substr(0,1) ;
if(start == ";")cout<<"HELL O WORLD"<<'\n'; // skip this line for
comments
int len = str.length();
for (int a=0; a<len; ++a)
str[a]=toupper(str[a]);
Tokenize(str, tokens);
for (int i=0;i<sizeof(&t okens);++i){ // check amount of tokens
cout << tokens[i]<<'\n';
}
}
Comment