尝试转换 str 字符串以在 std::getline 分隔符中使用

发布于 2025-01-04 05:34:16 字数 2720 浏览 0 评论 0原文

我试图获取 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* 转换有关。

 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'

更新:
找到了答案: 如何在 C++ 中标记字符串?
http://www.codeproject.com/Articles/23198/ C-String-Toolkit-StrTk-Tokenizer

im trying to take std::string value and use it as delimiter in std::getline() function
but no mater what i do the compiler gives me error.
this is what im trying to do :

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);
    }

the error im getting is always about 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'

UPDATE:
found the answer:
How do I tokenize a string in C++?
http://www.codeproject.com/Articles/23198/C-String-Toolkit-StrTk-Tokenizer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2025-01-11 05:34:16

getline() 的第三个参数是字符,而不是字符数组。请参阅。如果需要多个分隔符或由多个字符组成的分隔符,则不能使用此功能。

使用 find() 和 substr()。

The third argument of getline() is a char, not a char array. See this. You cannot use this function if you need multiple delimiters or a delimiter made of multiple characters.

Use find() and substr().

夜血缘 2025-01-11 05:34:16

std::string::c_str() 为您提供一个指向字符数组的指针。为了使用 c_str() 调用的结果填充字符数组,您需要使用 strcpy()strncpy() > 因为它们实际上复制了 C 样式字符串。

另外,您已将 delrr 声明为 1024 个字符指针的数组,而不是 1024 个字符,这真的是意图吗?

std::string::c_str() gives you a pointer to a character array. In order to fill a character array with the results of the call to c_str() you'll need to use strcpy() or strncpy() as those actually copy the C style string.

Also, you've declared delrr as an array of 1024 character pointers rather than 1024 characters, is that really the intent?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文