C++ 如何避免字符串数组中的重复条目?
在我的小项目中,我想制作一个小程序,在这个程序中,我必须存储无限数量的唯一字符串,但用户可以多次输入相同的唯一字符串。但在我的数组中,我只希望唯一的id只保存一次。简单地说,我不希望数组中有重复的数据。我想用C++做这个,但是不知怎么搞不懂逻辑?有人能帮帮我吗C++ 如何避免字符串数组中的重复条目?,c++,arrays,string,duplicates,C++,Arrays,String,Duplicates,在我的小项目中,我想制作一个小程序,在这个程序中,我必须存储无限数量的唯一字符串,但用户可以多次输入相同的唯一字符串。但在我的数组中,我只希望唯一的id只保存一次。简单地说,我不希望数组中有重复的数据。我想用C++做这个,但是不知怎么搞不懂逻辑?有人能帮帮我吗 #include <stdio.h> #include <iostream> #include <string> using namespace std; int main(){
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
string str[100],ch;
int i,j,n;
j=0;n=0;
//str[0]= "a";
do {
getline(cin,ch);
for (i=0;i <j; i++){
if (ch=str[i]){
cout << "duplicate" ;
}
str[i] =ch;
j++;
}
n++;
} while (n =100);
getchar();
}
我是C++的NoOB,所以请帮助我在这里
如果你想要维护一个唯一的字符串列表,那么最简单的办法就是使用正确的工具来完成这个任务;也就是说,一个集合而不是一个字符串数组 编辑: 如果您不需要像set那样对字符串集合进行排序,并且您可以使用它,那么使用unordered_set比set更合适。每次添加字符串时,set只会执行不必要的排序 编辑2: 集合是一个关联数组,这意味着一个给定键只能有一个元素。对于set,键是插入的字符串。如果多次插入同一密钥,则该密钥集中仍将只有一个实例 下面是一个示例程序来说明这一点。如果运行此命令,您会发现输出仅为一个foo,即使foo已插入三次:#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
set<string> my_strings;
my_strings.insert("foo");
my_strings.insert("foo");
my_strings.insert("foo");
copy( my_strings.begin(), my_strings.end(), ostream_iterator<string>(cout, "\n"));
}
如果您想维护一个唯一字符串的列表,那么最简单的方法就是使用适合该作业的工具;也就是说,一个集合而不是一个字符串数组 编辑: 如果您不需要像set那样对字符串集合进行排序,并且您可以使用它,那么使用unordered_set比set更合适。每次添加字符串时,set只会执行不必要的排序 编辑2: 集合是一个关联数组,这意味着一个给定键只能有一个元素。对于set,键是插入的字符串。如果多次插入同一密钥,则该密钥集中仍将只有一个实例 下面是一个示例程序来说明这一点。如果运行此命令,您会发现输出仅为一个foo,即使foo已插入三次:
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
set<string> my_strings;
my_strings.insert("foo");
my_strings.insert("foo");
my_strings.insert("foo");
copy( my_strings.begin(), my_strings.end(), ostream_iterator<string>(cout, "\n"));
}
您应该使用一个向量来保存字符串列表。例如,您可以使用集合http://www.cplusplus.com/reference/stl/set/. 此外,如果需要检查set对象上是否已经存在该字符串,则需要使用find方法进行检查: 我想这就是你所需要的
仅供参考:行:如果ch=str[i]{是完全错误的!您不是在比较!您是在赋值,请记住使用“==”而不是“=”。您应该使用一个向量来保存字符串列表。例如,您可以使用一个集合http://www.cplusplus.com/reference/stl/set/. 此外,如果需要检查set对象上是否已经存在该字符串,则需要使用find方法进行检查: 我想这就是你所需要的
仅供参考:行:如果ch=str[i]{完全错了!你不是在比较!你是在赋值,记住使用“==”而不是“=”。还没有编译过这个,但是类似的东西应该可以工作,也就是说,如果你想要一个更有效的解决方案,你应该使用一套或类似的c++sh方法来解决这个问题,但是从听起来,你似乎需要更多的基本建议
int main()
{
const int maxstrings = 100; // never use magic numbers, instead declare them as a constant
string str[maxstrings],ch; // should have other variable names that are more descriptive
int i,n = 0; // didn't see the need for j here, n contains number of unique strings
do
{
getline(cin,ch);
// you may want to check what is in ch first, to see if user doesn't want to enter 100 strings
bool duplicate = false;
for (i=0; !duplicate && i<n; ++i) // check among number of stored strings (n)
{
if (ch==str[i]) // use two '=' for equality i.e '=='
{
cout << "duplicate:" << ch << endl; // show the duplicate, user friendlier
duplicate = true;
}
}
// did we find a duplicate? no, then add to array
if ( !duplicate )
{
str[n++]=ch;
}
}
while ( n < maxstrings );
getchar();
}
我还没有编译过这个,但是类似的东西应该可以工作,也就是说,如果你想要一个更有效的解决方案,你应该使用一套或类似的c++sh方法来解决这个问题,但是从听起来,你似乎需要更基本的建议
int main()
{
const int maxstrings = 100; // never use magic numbers, instead declare them as a constant
string str[maxstrings],ch; // should have other variable names that are more descriptive
int i,n = 0; // didn't see the need for j here, n contains number of unique strings
do
{
getline(cin,ch);
// you may want to check what is in ch first, to see if user doesn't want to enter 100 strings
bool duplicate = false;
for (i=0; !duplicate && i<n; ++i) // check among number of stored strings (n)
{
if (ch==str[i]) // use two '=' for equality i.e '=='
{
cout << "duplicate:" << ch << endl; // show the duplicate, user friendlier
duplicate = true;
}
}
// did we find a duplicate? no, then add to array
if ( !duplicate )
{
str[n++]=ch;
}
}
while ( n < maxstrings );
getchar();
}
std::unordered_settd::unordered_setI建议使用std::unordered_set(如果有的话),std::set(如果没有)。用户会不断输入他们的电子邮件地址,但我只想在数组中以字符串形式存储一次,所以即使他们输入两次或更多次,它也不会存储在我的数组中,set可以这样做吗?如果可以,我建议使用std::unordered_set(如果有的话)你有,std::set if not。用户会不断输入他们的电子邮件地址,但我只想在数组中以字符串形式存储它一次,所以即使他们输入两次或更多次,它也不会存储在我的数组中,可以设置吗?Anders K。你能编译并检查它吗,它在这里不起作用,尽管一切似乎都是正确的。Anders K.你能编译并检查一下吗?虽然看起来一切正常,但它在这里不起作用。