如何将cin中的用户输入转换为C++11 std::array
How do I get user input from cin into C++11 std::array
如何将用户输入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();
- 如何将模板转换为C++11之前的模板
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 如何将Javascript正则表达式转换为C++11正则表达式
- 从c++11 time_t创建一个可转换为.net DateTime的字符串
- C++11 模板函数"implicity"将位集<N>转换为"unsigned long"
- 从迭代器转换为变量的类型.(C++11 模板)
- C++11中的自动类型扣除和类型转换有什么区别
- 如何将元组转换为C 11中的字节数组
- C 11:为什么RVALUE参考参数隐式转换为LVALUE
- ISO C++11 不允许从字符串转换为字符
- 在 C++11 中将静态 constexpr 数组转换为模板参数
- 尝试将 C++11 代码转换为 C++03 时默认函数模板参数出错
- 将字符串元素转换为整数 (C++11)
- 对C 98和C 11的停止隐式转换
- C++11 何时相对于运算符优先级进行算术类型转换
- C++11自动.从float转换为long
- C 11中函数参数的显式模板函数参数规范和隐式转换
- C++11 unique_ptr和shared_ptr能够转换为彼此的类型吗?
- 如何在不提升的情况下将 C++11 代码转换为 C++98
- 转换基数 11 到十进制