如何在C++中解析数组中的重复项
How do I parse arrays in C++ for duplicates
请帮助我使用C++解析数组,并且只显示唯一的数字。我写了一个程序,回答了下面的大部分问题。
问题:使用一维数组来解决以下问题。读20个数字,每个数字都在10到100之间,包括10到100。读取每个数字时,只有当它不是已读取数字的副本时,才验证它并将其存储在阵列中。读取所有值后,只显示用户输入的唯一值。提供所有20个数字不同的"最坏情况"。使用尽可能小的数组来解决此问题。
我所做的:我的程序创建了一个20项数组。提示用户输入数据,验证并显示。我已经尝试了几种只显示唯一数据的方法,但我没有完成问题的要求。
示例用户输入:
11、12、12、13、14、15、15、16、17、18、19、20、21、22、23、24、25、26、27
我的程序输出:
{11,12,12,13,14,15,15,16,17,18,19,20,21,22,23,24,25,26,27}
正确的程序输出:
{11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}
有什么建议吗。请参阅下面的代码。谢谢你的帮助!
#include <iostream>
#include <array> // needed for c++11 arrays
using namespace std;
int main()
{
const int arraySize = 20; // limit array length to 20
array <int, arraySize userArray> = {};
//Populate array with user input and validate it to be between 10 and 100
for (int i = 0; i < userArray.size(); i++)
{
cout << "Enter a number between 10 and 100" << endl;
cin >> userArray[i]; // get user input assign it to proper array subscript
while(userArray[i] > 100 || userArray[i] < 10)//validate user input to be between 10 and 100
{
cout << "Number needs to be between 10 and 100. Enter a new number" << endl;
cin >> userArray[i]; //reassign the proper array subscript if needed
}
}
cout << endl;
//display the information to look like an array
//result looks like [ v, w, x, y, z ]
cout << "[ ";
//display array values
for (int i = 0; i < userArray.size() - 1; i++)
{
cout << userArray[i] << ", ";
}
//properly display last array item
cout << userArray[(userArray.size() - 1)] << " ]" << endl;
return 0; }
如果您可以使用std::vector
,那么您可以使用以下解决方案。
template <typename Type>
std::vector<Type> unique_entries (std::vector<Type> vec) {
for (auto iter = vec.begin (); iter != vec.end (); ++iter) {
auto f = std::find_if (iter+1, vec.end (), [&] (const Type& val) {
return *iter == val; // (X)
});
if (f != vec.end ()) {
vec.erase (std::remove (iter+1, vec.end (), *iter), vec.end ());
}
}
return vec;
}
template <typename T>
void show (std::vector<T> vec) {
for (const auto& v : vec) {
std::cout << v << " ";
}
std::cout << std::endl;
}
例如:
std::vector<int> vec {11, 12, 12, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
std::vector<short> vec2 {1, 1, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 10};
std::vector<std::string> vec3 {"a", "a", "a", "aa", "b", "b", "bb", "c"};
show (vec);
show (unique_entries (vec));
show (vec2);
show (unique_entries (vec2));
show (vec3);
show (unique_entries (vec3));
输出:
11 12 12 12 13 14 15 15 16 17 18 19 20 21 22 23 24 25 26 27
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
1 1 1 2 3 2 1 2 1 2 3 2 1 2 3 2 1 2 10
1 2 3 10
a a a aa b b bb c
a aa b bb c
看看(X(行,基本上你可以将这个unique_entries
用于所有提供operator==
的类型,但你必须记住,如果你将这个函数用于浮点类型,这可能会失败。
希望这就是您想要的。。。。。。。。。。。基本上,我使用另一个数组来存储用户已经输入的元素。因此,当用户输入一个数字时,它会检查它是否已经在"输入的元素"数组中。
#include<iostream>
#define MAX 20
using namespace std;
int main()
{
int list[MAX],visitedElements[MAX];
int noElements,element,currentIndex;
bool flag=false;
cout<<"Enter total no. of elements(MAX=20):"<<endl;
cin>>noElements;
for(int i=0; i<noElements; i++)
{
visitedElements[i] = -1;
}
for(int i=0;i<noElements;i++)
{
currentIndex=i;
cout<<"Enter Element:"<<endl;
cin>>element;
for(i=0;i<currentIndex;i++)
{
if(visitedElements[i] == element)
{
flag==true;
break;
}
}
if(flag == false)
{
list[i]=element;
visitedElements[i] = element;
}
flag = false;
}
cout<<"Elements in list are"<<endl;
for(int i=0 ; i<noElements ;i++)
cout<<list[i]<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main(){
int tab[10];
int i=0,j;
int number=0;
int counter=1;
bool flag=true;
int tries=0;
while(tries<10){
do{
cin>>number;
tries++;
if((number>100)||(number<10)){
cout<<"Wrong number, has to be between 10 and 100"<<endl;
}
}
while((number>100)||(number<10));
flag=true;
if(i==0){
tab[i]=number;
}
else{
for(j=0; j<counter; j++){
if(tab[j]==number){
flag=false;
i--;
break;
}
}
}
if(flag==true){
tab[i]=number;
counter++;
}
i++;
}
for(i=0; i<counter-1; i++){
cout<<tab[i]<<", ";
}
}
我可能有点碰上了。我是编程新手,但这是我的解决方案。所以,你说它需要20位,我的需要10位,输入10位后,它会列出数组。所以,如果我的输入是"10,10,12,13,15,15,18,22,22"输出为:"10、12、13、15、18、22"我可能会擦除一半的代码,但就像我说的那样,我是一个初学者,我写得很匆忙。
EDIT:Ups,数组声明中的一个小错误。我在测试它的5个元素,忘记了制作一个更大的数组。
问题是使用一个数组,所以在读取每个数字后,我会循环遍历数组中已经存在的数字,并检查是否重复。它看起来像这样:
#include <iostream>
constexpr int ARRAY_SIZE = 20;
int main()
{
int userArray[ARRAY_SIZE], validNumbers = 0;
for(int i = 0; i < ARRAY_SIZE; i++)
{
int num;
bool isGood = true;
std::cin >> num;
//code to check if input is in range here
for(int j = 0; j < validNumbers; j++)
{
if(userArray[j] == num)
{
isGood = false;
break;
}
}
if(isGood)
{
userArray[validNumbers] = num;
validNumbers++;
}
}
//userArray now contains all unique numbers in the order that they were entered
for(int i = 0; i < validNumbers; i++)
{
std::cout << userArray[i];
if(i < validNumbers - 1)
{
std::cout << ' ';
}
}
std::cout << 'n';
}
在for循环中,不要让cin直接进入数组,而是添加另一个int变量并将其设置为等于输入
cin >> variable
然后创建一个for循环,在当前数组中进行迭代,以确保它不是重复的;使用下面的行作为for循环
for(int j = 0; j < i; j++)
如果数字已经在数组中,则继续。否则,将其添加到中
此外,我将在中使用while循环,而不是在一开始使用for循环
while(i < arraysize)
然后,每次看到重复时都要取消增量数组大小,并且在添加元素时只增加i
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '