C++;使用多字节分隔符读取iostream 我们知道从输入流中读取,我们可以使用以下标准C++函数 istream& getline (char* s, streamsize n, char delim );

C++;使用多字节分隔符读取iostream 我们知道从输入流中读取,我们可以使用以下标准C++函数 istream& getline (char* s, streamsize n, char delim );,c++,iostream,C++,Iostream,但是,我们只能提供一个字节/字符的分隔符 如果我希望使用具有多个字节的分隔符,我应该怎么做? 有什么好东西我可以利用吗 最佳, 我想你可以用cin.get()代替cin.getline()。每次读取一个字符,并测试是否出现分隔符 int main(void){ string-str; 分隔符的整数长度=3; 常量字符*分隔符=“ABC”; 字符温度='0'; bool over=false; coutBoost Spirit是一种可能性。@JerryCoffin是的。但只有在你有时间的情况下。@

但是,我们只能提供一个字节/字符的分隔符

如果我希望使用具有多个字节的分隔符,我应该怎么做? 有什么好东西我可以利用吗

最佳,

我想你可以用
cin.get()
代替
cin.getline()
。每次读取一个字符,并测试是否出现分隔符

int main(void){

string-str;
分隔符的整数长度=3;
常量字符*分隔符=“ABC”;
字符温度='0';
bool over=false;

coutBoost Spirit是一种可能性。@JerryCoffin是的。但只有在你有时间的情况下。@JerryCoffin,@MarkGarcia,谢谢你的话!你能给我一个使用它的简单例子吗?我不太熟悉它,谢谢!你需要在每次阅读之前用
cin.good()检查是否有数据
例如,我同意你的看法,这是我的最终解决方案。
string str;  
int length_of_delimiter = 3;  
const char *delimiter = "ABC";  
char temp = '0';  
bool over = false;  
cout<<"Enter the stream"<<endl;

temp =  cin.get();
int  i = 0;
while(over == false){ 
        for(i = 0; temp  == delimiter[i] && i < length_of_delimiter; i++){
            str += temp;
            temp = cin.get();
        }
        if(i == length_of_delimiter){
            //chop off the delimiter
            str.erase(str.end() - length_of_delimiter, str.end());
            over = true;
        }
        else {
            str += temp;
            temp = cin.get();
        }
}
cout<<"The stream we wanted is: "<<str<<endl;
return 0;