我试图获取std::string
值并将其用作std::getline()
函数中的分隔符
但没有我做什么编译器给我错误。
这就是我想要做的事情:
std::stringstream ss(s);
std::string item;
std::string delim ="&&=";
int ssize = delim.size();
int newssize = ssize+1;
char del[SSIZE]; // also here when i try gives error
strcpy(del,delim.c_str());
char * delrr[1024] = delim.c_str(); //gives error
while(std::getline(ss,item,delrr)) {
elems.push_back(item);
}
错误即时获取总是关于const char * convertion。
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *[1024]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>.\UT.cpp(179) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> d:\Program Files\Microsoft Visual Studio 9.0\VC\include\string(527) : see declaration of 'std::getline'
1>.\UT.cpp(179) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::stringstream'
1> d:\Program Files\Microsoft Visual Studio 9.0\VC\include\string(475) : see declaration of 'std::getline'
1>.\UT.cpp(179) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::stringstream'
1> d:\Program Files\Microsoft Visual Studio 9.0\VC\include\string(475) : see declaration of 'std::getline'
1>.\UT.cpp(179) : error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
1> d:\Program Files\Microsoft Visual Studio 9.0\VC\include\string(475) : see declaration of 'std::getline'
1> could be 'char **'
1> or 'char'
更新:
找到了答案:
How do I tokenize a string in C++?
http://www.codeproject.com/Articles/23198/C-String-Toolkit-StrTk-Tokenizer
答案 0 :(得分:4)
getline()的第三个参数是char,而不是char数组。见this。如果您需要多个分隔符或由多个字符组成的分隔符,则无法使用此功能。
答案 1 :(得分:3)
std :: string :: c_str()为您提供指向字符数组的指针。为了使用c_str()
调用的结果填充字符数组,您需要使用strcpy()
或strncpy()
,因为这些实际上会复制C样式字符串。
此外,您已将delrr
声明为1024个字符指针而非1024个字符的数组,这是真的意图吗?