如何将输入存储到数组中?C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6921188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to store an input into an array? C++
提问by Junior89
int b;
int array[12];
cout << "Enter binary number: ";
cin >> b;
(for example: b will be 10100)
(例如:b 将是 10100)
** How to store b(10100) into an array so that it'll be [1][0][1][0][0] **
** 如何将 b(10100) 存储到数组中,使其为 [1][0][1][0][0] **
cout << array[0] << endl;
** output should be 1 **
** 输出应该是 1 **
cout << array[1] << endl;
** output should be 0 **
** 输出应为 0 **
Please help thank you.
请帮忙谢谢。
回答by Pablo
A string
can also be treated as an array of char
s. So you can get input into a string, instead, and the cout
statements you wrote, should work. However, they will be char
s and not ints, so you would be storing '1' and '0' instead of 1 and 0. Converting between them is easy, just use array[0]-'0'
Astring
也可以被视为char
s的数组。因此,您可以将输入输入到字符串中,而cout
您编写的语句应该可以工作。但是,它们将是char
s 而不是整数,因此您将存储 '1' 和 '0' 而不是 1 和 0。它们之间的转换很容易,只需使用array[0]-'0'
#include <string>
#include <iostream>
using namespace std;
int main()
{
string array;
cout << "Enter binary number: "; cin >> array;
// Suppose the user inputs 10100
cout << array[0] << endl; // outputs '1'
cout << array[1] << endl; // outputs '0'
cout << array[2] << endl; // outputs '1'
return 0;
}
Update: Added compilable code. Note that this is pretty much the original code posted with the question, except for inputting a string
.
更新:添加了可编译代码。请注意,这几乎是随问题发布的原始代码,除了输入string
.
回答by Martin York
You can use boots dynamic bitset
您可以使用 boots dynamic bitset
#include "boost/dynamic_bitset.hpp"
#include <sstream>
#include <iostream>
int main()
{
boost::dynamic_bitset<> val;
std::stringstream input("1010101010");
input >> val; // Input a binary number
// Can use std::cin or another stream
std::cout << val.to_ulong() << "\n";
std::cout << val[5] << "\n";
}
If you don't have boost use the std::bitset.
The only problem with std::bitset it has a fixed size
如果您没有 boost,请使用 std::bitset。
std::bitset 的唯一问题是它的大小是固定的
#include <bitset>
#include <sstream>
#include <iostream>
int main()
{
std::bitset<16> val; // fixed 16 bit size
std::cout << "Enter binary number:\n";
std::cin >> val; // Input a binary number
// Can use std::cin or another stream
std::cout << val.to_ulong() << "\n";
std::cout << val[5] << "\n";
}
回答by ssell
I had originally wrote up an answer similar to Pablo, but seeing as he already posted it, here is one more consistent with the information given.
我最初写了一个类似于 Pablo 的答案,但看到他已经发布了,这里有一个更符合所提供信息的答案。
It takes an int
input and places it into an int array
.
它接受一个int
输入并将其放入一个int array
.
#include <iostream>
#include <cmath>
int main( )
{
int b;
int numDigits;
// Get bit string int
std::cin >> b;
// Get the number of digits
numDigits = std::floor( std::log10( ( float )std::abs( b != 0 ? b : 1 ) ) ) + 1;
// Dynamically create a new array of the appropriate size
int* arr = new int[ numDigits ];
// Initialize all the blocks of memory
std::memset( arr, 0, numDigits );
// Fill the array
for( int i = 0; i < numDigits; i++ )
{
arr[ numDigits - i - 1 ] = b % 10;
b /= 10;
}
system( "PAUSE" );
// Delete the array
delete [] arr;
return 0;
}
This one dynamically sets the size of the array so it fits correctly.
这个动态设置数组的大小以使其正确匹配。
回答by Eric Z
The following example stores bits to a raw C-style array as you require. But you can replace it with a std::vector if you want.
以下示例根据需要将位存储到原始 C 样式数组。但是如果你愿意,你可以用 std::vector 替换它。
int main()
{
// assume sizeof(int) is 32, or you can use heap-allocated array or std::vector
int array[32];
unsigned int mask = 1;
// mask is initially 0x80000000
mask = mask << (sizeof(int)*8 - 1);
int i = 0;
// we start counting from the first "1",
// preceding "0"s are ignored to display
bool started = false;
int b;
cin >> b;
while (mask != 0)
{
// if current bit is "1" or "0"
if (mask & b)
{
started = true;
array[i++] = 1;
}
// if current bit is "0" and counting started
else if (started)
{
array[i++] = 0;
}
// ready to test next bit
mask = mask >> 1;
}
// test result
for (int j = 0; j < i; ++j) cout << array[j];
cout << endl;
return 0;
}
Test cases:
1. b = 8 => array: 1000
2. b = -8 => array: 11111111111111111111111111111000
3. ..
回答by Brandon
STORING A BINARY NUMBER INTO AN ARRAY
将二进制数存储到数组中
char binaryArray [5]; // The array looks like this: [][][][][]
cout << "Enter binary number: ";
cin >> binaryArray; // Stores the binary number into the array: [1][0][1][0][0]
cout << binaryArray[0] << endl; // The first element is sent to standard output.
cout << binaryArray[1] << endl; // The second element is sent to standard output.
For your input the output will be:
对于您的输入,输出将是:
1
0
Here we have an array of characters. We input the binary number into the array and then we print each element of the array to display each bit. The first print line accesses the first element [1][0] [1] [0] [0]. The second print line accesses the second element [1] [0][1] [0] [0].
这里我们有一个字符数组。我们将二进制数输入数组,然后打印数组的每个元素以显示每个位。第一个打印行访问第一个元素[1][0] [1] [0] [0]。第二个打印行访问第二个元素 [1] [0][1] [0] [0]。
Let's say we had a binary number with 100 characters. The way that we output each element of the array would take a long time.
假设我们有一个包含 100 个字符的二进制数。我们输出数组的每个元素的方式需要很长时间。
1.) Can you think of a more efficient way of printing out the contents of our array?
1.) 你能想出一种更有效的方式来打印出我们数组的内容吗?
2.) How can we ensure that the user is inputting only ones and/or zeroes?
2.) 我们如何确保用户只输入 1 和/或 0?