c++数组(无限制输入)

C++ Arrays (Unlimited Input)

本文关键字:输入 无限制 数组 c++      更新时间:2023-10-16

我想做的是允许用户输入任意数量的变量(例如:1 6 945 fhds),我的程序将检查任何字符串。我听说过数组,但我觉得我只能限制用户可以有多少输入。Foo似乎需要预先输入编码到程序中?有人能说清楚怎么做吗?

I tried:

#include <iostream>
using namespace std;
int main() {
int x;
int array[x];
cout << "Enter your numbers: ";
for (int x = 0; x = <char>; ++x) {
cin >> x;
}
cout << x; 
return 0;
}

在c++中,可调整大小的数组称为"vector":

vector<int> array;
cout << "Enter your numbers: n";
int temp;
while (cin >> temp)
    array.push_back(temp);
cout << array.size() << 'n';

c++并不是一门容易上手的语言。这里有很多需要注意的问题和设计模式,你需要对它们进行大量的研究,以熟悉它们而不会造成伤害。user4581301建议的"大书单"是一个很好的开始。

网上也有一些很好的参考资料,但更多的是不好的参考资料。

例如,你的问题有几个问题(除了已经提到的初始化等问题)。

我将尝试从更高的层次来解决上面的两个问题。

  1. 我建议在的情况下使用数组。在可能的情况下,选择不同的抽象。明智地选择数据类型。例如,这里有一些标准容器。你也可以利用boost。您可以使用指针并将其视为数组,在这种情况下,您可以完全控制内存的管理。您甚至可以使用混合方法,使用像vector这样的连续容器,并通过指针访问它。然而,指针和数组相当脆弱,容易出错。在面对异常时,它们更是如此,我建议始终首先考虑RAII原则。

  2. 根据您计划读取的数据,您可能需要考虑您正在使用的类型。您可能还需要考虑编码。我想看看utf-8,以便更好地了解字符串的世界。

到目前为止还存在一些问题,比如多次声明和误用变量'x';for循环可能不应该使用x,因为它已经定义,并且x在未初始化时不应该是数组大小。

至于你想做什么,这很难说,但我想我能帮上忙。

int main()
{
    const int ARRAY_SIZE = 1000; //initialize so it doesn't end up being either 0 or literally anything
    int myArray[ ARRAY_SIZE ];
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        cin >> myArray[ x ];
    }
    return 0;
}

现在这将循环1000次,要求一个数字输入,直到数组写满,如果你想要能够停止数组,你必须添加一个方法来中断for循环,并记录它停止的位置。

int main()
{
    const int ARRAY_SIZE = 1000;
    int myArray[ ARRAY_SIZE ];
    int arrayEnd = 0;
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        int inputBuffer = 0; // this variable saves the users input and checks to see if the user wants to exit before saving the variable to myArray.
        cin >> inputBuffer;
        if( inputBuffer != -1 ) // -1 is just a value the user enters to stop the loop, choose any number you want for this.
        {
            myArray[ x ] = inputBuffer;
        }
        else
        {
            arrayEnd = x;
            break; // stops the for loop if the number above is entered.
        }
    }
    if( arrayEnd == 0 )
    {
        arrayEnd = ARRAY_SIZE;
    }
    return 0;
}

如果你想要真正无限的或者一个更具可塑性的整型数组,你可以new一个整型数组,像这样设置数组的大小

int main()
{
    int* myArray = nullptr;
    int arraySize = 0;
    cin >> arraySize;
    myArray = new int[ arraySize ];
    for( int x = 0; x < arraySize; x++ )
    {
        cin >> myArray[ x ];
    }
    delete[] myArray;
    return 0;
}

但是我不建议使用new,如果你没有基本的东西,因为new很容易导致内存泄漏和更多的小事情需要跟踪。

  1. 读取全行
  2. 按空格分割得到输入
  3. 检查每个输入的类型

.........

#include <string>
#include <iostream>
#include <vector>
#include <sstream>

std::vector<std::string> split(const std::string &s) {
    std::stringstream ss(s);
    std::string item;
    std::vector<std::string> elems;
    while (std::getline(ss, item, ' ')) {
        elems.push_back(item);
    }
    return elems;
}
int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::vector<std::string> elems = split(line); 
    for (int i = 0; i < elems.size; i++)
    {
        /*
        if is number 
          do something
        else
            ignore
        */
    }   
}