如何将cin中的用户输入转换为C++11 std::array

How do I get user input from cin into C++11 std::array

本文关键字:转换 C++11 std array 输入 用户 cin      更新时间:2023-10-16

如何将用户输入std::array

这是我所拥有的,但它不会编译。

std::array<char, 10> myArray{"hello"} ;
std::cout << "Enter your name: ";
std::cin >> myArray;

如果输入的字符超过10个,请截断并忽略它们。我还需要清除cin缓冲区,以便稍后进行其他输入。

对于当前示例,应该使用std::string而不是std::array<char, 10>。然而,如果你仍然想读取一个数组,你可以按照以下方式进行:

#include <iostream>
#include <array>
int main() {
    std::array<int, 10> arr;
    for(int temp, i = 0; i < arr.size() && std::cin >> temp; ++i) {
        arr[i] = temp;
    }
    for(auto&& i : arr) {
        std::cout << i << ' ';
    }
}

输出:

$ ./a.out
10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 16 17 18 19

基本思想是循环直到数组大小,然后使用其operator[]将其插入到数组中。将std::cin >> temp保持在循环条件下,以捕获插入过程中的错误。请注意,在标准库中没有一个内置函数可以为您实现这一点。

如果你发现自己经常这样做,你可以把它转移到你自己的功能中:

#include <iostream>
#include <array>
template<typename T, size_t N>
std::istream& input_array(std::istream& in, std::array<T, N>& arr) {
    unsigned i = 0u;
    for(T temp; i < arr.size() && in >> temp; ++i) {
        arr[i] = std::move(temp);
    }
    return in;
}
int main() {
    std::array<int, 10> arr;
    input_array(std::cin, arr);
}

您不应该为std命名空间内的内容重载operator>>,因为这是未定义的行为。

如果你想避免临时的,你可以修改功能如下:

template<typename T, size_t N>
std::istream& input_array(std::istream& in, std::array<T, N>& arr) {
    for(unsigned i = 0u; i < arr.size() && in >> arr[i]; ++i) { 
        // empty body
    }
    return in;
}

只需为作业使用适当的工具:

std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);

现场演示

需要<iostream><limits>标头。

std::cin.get(myArray.data(), myArray.size());
// get() will actually read up to myArray.size() - 1 chars
// and add a null character at the end ('')
if (std::cin.fail()) {
    // could not read any characters or EOF occurred
} else {
    // drop rest of line that was read to buffer
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
    // number of characters read: std::cin.gcount()
    // ... use myArray ...
}

方法参考:

http://en.cppreference.com/w/cpp/io/basic_istream/get

http://en.cppreference.com/w/cpp/io/basic_ios/fail

http://en.cppreference.com/w/cpp/io/basic_istream/ignore

http://en.cppreference.com/w/cpp/io/basic_istream/gcount

您可以将整行数据放入缓冲区,然后将该缓冲区的前10个字节(如果小于10,则提取整个缓冲区)提取到数组中。这也解决了std::cin上没有任何垃圾的问题。

std::array<char, 10> myArray{"hello"} ;
std::string s;
std::getline( std::cin, s );
if ( !std::cin )
     throw.....   // input was closed before a line was entered
std::size_t len = std::min( myArray.size(), s.size() );
std::copy(s.begin(), s.begin() + len, myArray.begin() );

请注意,这不会在myArray中创建以null结尾的字符串。如果要执行此操作,请复制一个额外的字符(并使len最多为myArray.size() - 1)。

可能需要包括<array><iostream><algorithm>

std::cin在这种情况下需要一个字符指针(char*)。所以您需要使用.data()

std::array<char, 50> arr{"Hello, "};
size_t hello_length = strlen(arr.data());
std::cout << "Enter your name: ";
std::cin >> std::setw(10) >> (arr.data() + hello_length);
std::cout << arr.data() << std::endl;

在C++中,数组的指针是第一个元素的地址
对于std::array,我们可以通过数组的指针来获取输入。

std::cin >> myArray.begin();