C++ 计数冗余字符串
c++ count redundant string
#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;
}
这是一个简单程序的代码,用于计算有多少字符串输入是冗余的。一切似乎都很好,除了我需要输入两次文件末尾(ctr+z(才能结束循环并获得结果。我不知道为什么会发生这种情况。
您似乎正在尝试在行序列的末尾输出 EOF 字符:
> This is my inputEOF
这将迫使您输入另一个EOF
以实际结束流。 如果要使用单个 EOF 结束流,则需要先按回车键:
> This is my inputENTER
> EOF
如果使用std::set
,则可以显著简化代码
int main()
{
cout << "Input strings(end-of-file to exit):"<<endl;
set<string> strings;
string x;
int same_string=0;
while (cin >> x)
{
if( !strings.insert(x).second ) ++same_string;
}
cout << "Same string number:" << same_string << endl;
system("pause");
return 0;
}
相关文章:
- 如何擦除冗余输入?
- 使用提升几何缓冲区缩放多边形时的冗余折点
- 我应该担心冗余声明吗?
- Qt的slot(?)将冗余命名空间插入连接
- 方法冗余移动调用的移动语义
- 使用 llvm 和本地值编号算法擦除冗余表达式
- 优化堆上创建的冗余变量
- 如何消除访客模式冗余?
- 表达式模板需要冗余重载
- C++,处理多个构造函数重载和冗余代码
- 使用模板基类消除工厂类派生类冗余的干净方法
- 连接两个文件时如何避免冗余尾随换行符?
- CMake:修改共享库时的冗余链接
- 提升精神 X3:"属性没有预期的大小"冗余
- 如何简化此冗余C++代码?
- 是``if`语句''在modulo之前和分配操作之前的冗余
- 使用最终类说明符时,最终函数说明符是否冗余?
- 简化冗余std :: array初始化,当时没有constexpr构造函数
- C++ 计数冗余字符串
- 使用具有冗余信息的键字符串的映射