Getline和cin在C ++中产生不同的输出 [英] Getline and cin producing different output in C++
问题描述
我必须计算SUVO和SUVOJIT出现在长串中的次数。
输入=>
5分配
SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT
AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT
SUVOJITPRQSUVOJIT
SUVOJITTXGSUVOUNSUVOJIT
SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM
在下面的程序中,当我使用getline获取字符串输入时,它产生的输出包括SUVO = 0,SUVOJIT = 0,总是在第一行。而cin>>并没有产生这种行为。为什么?
#include < iostream >
#include < string >
使用 命名空间标准;
int main(){
int n;
cin>> n;
string s;
for ( int i = 0 ; i< n; i ++){
cin>> s; // getline(cin,s)
int s1 = 0 ,s2 = 0 ;
for ( int j = 0 ; j< s.length(); j ++){
if (s [j] == ' S'){
if (s.substr(j,< span class =code-digit> 7 )。compare( SUVOJIT) == 0 )
s1 ++;
else if (s.substr(j, 4 )。compare( SUVO)== 0 )
s2 ++;
}
//
}
//
cout<< SUVO =<< s2<< ,SUVOJIT =<< s1<< endl;
}
return 0 ;
}
我的尝试:
输入=>
5分配
SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT
AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT
SUVOJITPRQSUVOJIT
SUVOJITTXGSUVOUNSUVOJIT
SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM
那是因为 getline(string) - C ++ Reference [ ^ ]和 istream的::运营商GT;> - C ++参考a> [ ^ ]表现不同。
getline()
读取分隔字符(换行符<$ c $读取c> \ n 默认情况下)并丢弃该字符。
istream>>
带字符串的运算符会跳过前导空格(如空格,制表符,换行符)并读取,直到遇到空格字符。该空格字符未被读取但留在输入缓冲区中。
您正在调用
cin>> ; N;最初。这将读取输入的数字,但会将换行符放在输入缓冲区中。之后调用
getline()
时,将读取换行符作为第一个字符并停止。那就是:你有一个空字符串。当使用istream>>
运算符时,它将跳过换行符并读取下一行的内容。
由于这种不同的行为,建议不要混用getline()
和>>
操作员调用相同的流。
你应该注意SUVO是SUVOJIT的一部分,因此SUVOJIT也是SUVO和的出现CANT 没有SUVO。
如果 (s [j] == ' S'){
if (s.substr(j, 4 )。compare( SUVO)== 0 ){
s1 ++;
if (s.substr(j, 7 )。compare( SUVOJIT)== 0 ) // 删除其他
s2 ++;
}
}备注:在输入之前为用户输出一些输出,而确实不会将s1或s2用于int count => count1和count2。
I have to count the no.of times SUVO and SUVOJIT appears in the long string.
Input=> 5 SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT SUVOJITPRQSUVOJIT SUVOJITTXGSUVOUNSUVOJIT SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM
In the below program when I take getline for taking string input the output it is generating consist of SUVO=0,SUVOJIT=0 in the first Line always .While the cin>> is not producing this kind of behaviour .Why?
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin>>n;
string s;
for(int i=0;i<n;i++){
cin>>s; //getline(cin,s)
int s1=0,s2=0;
for(int j=0;j<s.length();j++){
if(s[j] == 'S'){
if(s.substr(j,7).compare("SUVOJIT") == 0)
s1++;
else if(s.substr(j,4).compare("SUVO") == 0)
s2++;
}
//
}
//
cout<<"SUVO="<<s2<<", SUVOJIT="<<s1<<endl;
}
return 0;
}
What I have tried:
Input=> 5 SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT SUVOJITPRQSUVOJIT SUVOJITTXGSUVOUNSUVOJIT SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM
That is because getline (string) - C++ Reference[^] and the istream::operator>> - C++ Reference[^] behave differently.
getline()
reads until the delimiting character (newline\n
by default) is read and discards that character.
Theistream >>
operator with strings skips leading white spaces (like space, tab, newline) and reads then until a white space character is encountered. That white space character is not read but left in the input buffer.
You are calling
cin>>n;initially. That will read the entered number but will let the newline character in the input buffer. When calling
getline()
afterwards, that will read the newline as first character and stop. That is: you got an empty string. When using theistream >>
operator instead, it will skip the newline character and read the content of the next line.
Due to this different behaviour it is not recommended to mixgetline()
and>>
operator calls for the same stream.
You should pay attention that "SUVO" is a part of "SUVOJIT" and so "SUVOJIT" is also an occurance of "SUVO" and CANT occur without"SUVO".
if(s[j] == 'S'){ if(s.substr(j,4).compare("SUVO") == 0) { s1++; if(s.substr(j,7).compare("SUVOJIT") == 0) //removed else s2++; } }Remark: Make some output for the user before input and I really wouldnt use s1 or s2 for a int count => count1 and count2.
这篇关于Getline和cin在C ++中产生不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!