C++ c++;计数冗余字符串 #包括 #包括 #包括 使用std::cout;使用std::endl; 使用std::cin;使用std::vector;使用std::string; int main() { cout
似乎您正试图在一个行序列的末尾输出EOF字符:C++ c++;计数冗余字符串 #包括 #包括 #包括 使用std::cout;使用std::endl; 使用std::cin;使用std::vector;使用std::string; int main() { cout,c++,string,C++,String,似乎您正试图在一个行序列的末尾输出EOF字符: #include <iostream> #include <vector> #include <string> using std::cout; using std::endl; using std::cin; using std::vector; using std::string; int main() { cout << "Input strings(end-of-file to e
#include <iostream>
#include <vector>
#include <string>
using std::cout; using std::endl;
using std::cin; using std::vector; using std::string;
int main()
{
cout << "Input strings(end-of-file to exit):"<<endl;
vector<string> strings;
string x;
while (cin >> x)
{
strings.push_back(x);
}
typedef vector<string>::size_type vc_sz;
vc_sz size = strings.size();
int same_string=0;
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (strings[i] == strings[j])
++same_string;
}
}
cout << "Same string number:" << same_string << endl;
system("pause");
return 0;
}
这将迫使您输入另一个EOF
以实际结束流。如果您想用一个EOF结束流,您需要先按enter键:
> This is my inputEOF
似乎您正试图在一个行序列的结尾处输出EOF字符:
#include <iostream>
#include <vector>
#include <string>
using std::cout; using std::endl;
using std::cin; using std::vector; using std::string;
int main()
{
cout << "Input strings(end-of-file to exit):"<<endl;
vector<string> strings;
string x;
while (cin >> x)
{
strings.push_back(x);
}
typedef vector<string>::size_type vc_sz;
vc_sz size = strings.size();
int same_string=0;
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (strings[i] == strings[j])
++same_string;
}
}
cout << "Same string number:" << same_string << endl;
system("pause");
return 0;
}
这将迫使您输入另一个EOF
以实际结束流。如果您想用一个EOF结束流,您需要先按enter键:
> This is my inputEOF
如果使用
std::set
> This is my inputENTER
> EOF
intmain()
{
如果使用std::set
> This is my inputENTER
> EOF
intmain()
{
coutwhile(cin>>x)
可能做不到你认为它能做的!!看…可能是@g-makulik的重复。事实并非如此,此后不再使用cinloop@g-makulik这不是那个问题的重复。那个问题是问为什么他不能在Ctrl-Z之后输入…这个问题是问为什么他必须提供2个Ctrl-Z来中断输入。建立EOF您有两个选择。在空行上输入一个Ctrl-Z,或在内容行末尾输入两个Ctrl-Z。选择权归您。while(cin>>x)
可能做不到你认为它能做的!!看…可能是@g-makulik的重复。事实并非如此,此后不再使用cinloop@g-makulik这不是那个问题的重复。那个问题是问为什么他不能在Ctrl-Z之后输入…这个问题是问为什么他必须提供2个Ctrl-Z来中断输入。建立EOF您有两个选择。在空行上输入一个Ctrl-Z,或在内容行末尾输入两个Ctrl-Z。选择权归您。注意,在受支持的操作系统(Linux、OS X等)上,Ctrl-D的行为方式相同。我运行了问题中的代码,发现另一件奇怪的事情:如果输入是:like like hateEOF hateEOF love EOF(在新行中)结果是:相同的字符串编号:1,但它是2!!!您能解释一下吗???@T-D您必须清除EOF标记(std::cin.clear()
)后的错误状态。您还需要在尝试重新读取它之前忽略EOF字符。请记住,以这种方式使用EOF(意思是“流结束”)实际上应该意味着输入流的结束。将其视为条目之间的中断不是好的做法。注意,在受支持的操作系统(Linux、OS X等)上Ctrl-D的行为方式相同。我运行了问题中的代码,发现了另一件奇怪的事情:如果输入是:like like hateEOF hateEOF love EOF(在新行中)结果是:相同的字符串编号:1,但它是2!!!您能解释一下吗???@T-D您必须清除EOF标记(std::cin.clear()
)后的错误状态。您还需要在尝试重新读取它之前忽略EOF字符。请记住,以这种方式使用EOF(意思是“流结束”)实际上应该意味着输入流的结束。将其视为条目之间的中断不是好的做法。