使用基于C++11范围的循环验证数组值?[无矢量]
Verifying array values with C++11 range based loop? [Without vectors]
我有一个赋值,要求程序从用户输入到数组中读取20个数字。条件要求该值在10-100之间且不重复。我也只能使用一个包含20个元素的数组。然而,它不应该提示用户,只是不存储值;最后,程序必须打印出唯一的用户值。正确的结果例如:
input = 9 10 15 15 15 0
output = 10 15
//this is a small example with a 6 element array instead of 20
当我测试我的程序时,我只得到
input: 9 10 15 15 15 0
output: 10 15 15 15
//this is a small example with a 6 element array instead of 20
我使用基于范围的循环来检查值,如果不满足条件,则将值设置为0。所以任何不为零的东西都不会被打印出来。我已经研究了所有关于堆栈溢出的问题,但我找不到特定问题的答案:
- 如何使用类构造函数将所有数组元素初始化为零
- 使其"静态",这样当我运行另一个函数时,以前的数组值是全局的,并根据用户输入进行维护
我创建的循环似乎有问题,但看起来很完美。我和同学们核实了一下,他们也同意了
//arrayinput.h #include <array> #include <string> class arrayelimination { public: const static size_t limit = 20; arrayelimination(); void inputArray(); void displayArray(); private: std::array < int , limit > store; int userinput; }; //arrayinput.cpp #include <iostream> #include <array> #include "arrayinput.h" using namespace std; arrayelimination::arrayelimination() { array < int , limit> store = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; } void arrayelimination::inputArray() { for ( size_t i = 0; i < store.size(); i++) { cout << "Enter number between 10-100 for array box [" << i << "]: " ; cin >> userinput; //check if numbers is between 10-100 if (userinput >= 10 && userinput <= 100) { //MOST LIKELY ERROR check if number has previously been used. for ( int &check : store) { if ( check != userinput) { store[i] = userinput; break; } else store[i] = 0; } } //output if number isn't between 10-100 else store[i] = 0; } } void arrayelimination::displayArray() { cout << "Your unique array numbers stored include...n"; //output all the unique numbers that the user inputted. for ( size_t j = 0; j < 20; j++) { //if the value is NOT 0, output. if (store[j] != 0) { cout << "array[ " << j << " ] = " << store[j] << "n"; } } }
当我测试我的程序时,我只得到
input: 10 15 15 15 2 0 0 0 0 0 0 0 ... 0
output: 10 15 15 15
将其设置为零的概念是可行的,但重复的值并不是唯一的。
我必须使用面向对象的设计作为这项任务的要求。我快要死了——我真的不知道这是怎么回事。请帮帮我。
PS:糟糕的是,我忘了提到我只能使用一个数组
问题不在于range-based for loop
本身的结构,而在于检查输入值是否唯一的条件。
在此代码块中:
for ( int &check : store)
{
if ( check != userinput)
{
store[i] = userinput;
break;
}
else
store[i] = 0;
}
您正在设置userinput
遇到不匹配的ANY元素的值。因此,第一个不匹配的元素将导致您设置userinput
,即使userinput
稍后将在数组中匹配一个值。您需要确定的是userinput
与数组中的NO元素匹配。
例如:
for ( int &check : store)
{
if(check == userinput)
{
store[i] = 0;
break;
} // a match is found so set the value to 0 and stop
else
{
store[i] = check;
}
}
标准算法有什么问题吗?
int values[20];
// 1) read them in
for (int& v : values) {
std::cin >> v;
}
// 2) sort/uniq
std::sort(std::begin(values), std::end(values));
auto last = std::unique(std::begin(values), std::end(values));
// 3) print them
std::for_each(values, last, [](const int v){
std::cout << v << " ";
});
您让这件事变得过于复杂。
- 创建空矢量。甚至不要填充/调整它的大小
- 使用push_back添加新元素。根据需要添加任意数量
- 使用std::Sort对数组进行排序
- 遍历数组,并仅在以下情况下打印元素:
4.1.它是第一个元素[i==0]
4.2.或者它不等于前一个元素
您也可以对基于范围的循环执行同样的操作。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何在C++中检查2D数组中负值的输入验证
- 在C++中使用字符数组进行输入验证
- C++中的字符数组验证
- 在C++中使用字符数组进行用户输入验证
- C++ 中字符数组的输入验证
- C++中输入字符数组的特定验证
- 队列验证层错误:队列族索引在 pCreateInfo->pQueueCreateInfos 数组中不是唯一的
- 如何使用数组字符串进行输入验证
- C++ 在将输入值添加到数组之前对其进行验证
- 在数组中输入5个元素,检查验证,然后输出C++
- 使用基于C++11范围的循环验证数组值?[无矢量]
- 验证内存设置为默认值后数组中的更改
- C++ 字符数组大小验证
- 在 C 中验证数组的大小
- 数组边界检查和输入验证
- 验证数组没有重复的值