【发布时间】:2015-01-03 04:31:28
【问题描述】:
...可能是一个很简单的问题,但我将编写一个简单的 C++ 代码来使用分隔符解析字符串,我希望分隔符包含多个空格(实际上是一个或多个空格)。我的问题是,有可能这样做吗?我的示例代码是:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <sstream>
using namespace std;
int main()
{
string str="HELLO THIS IS 888and777";
char buf[1000];
getline(buf, 1000);
string str(buf);
stringstream stream(buf);
string toStr;
getline(stream, toStr,' ');//here the delimiter is six spaces
string str1=tostr;
getline(stream, toStr,' ');//here the delimiter is two spaces
string str2=tostr;
getline(stream, toStr,' ');//here the delimiter is three spaces
string str3=tostr;
cout<<str1<<"\t"<<str2<<"\t"<<str3<<endl;
return 0;
}
但是,我不能使用多个字符的分隔符。请有任何想法。 我收到以下错误:
error: invalid conversion from ‘void*’ to ‘char**’
error: cannot convert ‘std::string’ to ‘size_t*’ for argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
【问题讨论】:
-
您不能为此使用
getline。您也不能在单引号之间使用多于(或少于)一个字符。' '会比你说“实现定义的行为”更快地解雇你。
标签: c++ linux delimiter getline