如何将输入存储到数组中?c++
how to store an input into an array? C++
int b;
int array[12];
cout << "Enter binary number: ";
cin >> b;
(例如:b将是10100)
**如何将b(10100)存储到数组中,使其为[1][0][1][0][0]**
cout << array[0] << endl;
**输出应为1 **
cout << array[1] << endl;
**输出应为0 **
请帮忙谢谢。
A string
也可以视为char
s的数组。所以你可以得到一个字符串的输入,而你写的cout
语句,应该是有效的。然而,它们将是char
而不是int,因此您将存储'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:添加可编译代码。注意,除了输入string
之外,这几乎是与问题一起发布的原始代码。
我最初写了一个类似于Pablo的答案,但看到他已经发布了,这里有一个更符合给出的信息。
它接受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;
}
这一个动态设置数组的大小,使其正确适合
下面的示例根据需要将位存储到一个原始的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. ..
你可以使用boots动态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";
}
如果没有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";
}
将二进制数存储到数组
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.
对于您的输入,输出将是:
1
0
这里有一个字符数组。我们将二进制数输入到数组中,然后打印数组的每个元素来显示每个位。第一行打印访问第一个元素[1][0][1][0]。第二个打印行访问第二个元素[1][0] [1] [0] [0].
假设我们有一个100个字符的二进制数。我们输出数组中每个元素的方式将花费很长时间。
1。)你能想到一种更有效的方法来输出数组的内容吗?
2。)我们如何确保用户只输入1和/或0 ?
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '