假设VC ++控制台中输入了以下数字(与空格分开)。 N可能是10,20或100,这是不确定的。
1 2 3 4 ... N [Enter]
输入数量不确定,可能是10或20.按 Enter 键后,如何将这些数字放入数组?
array[0]=1; array[1]=2; ...
如何使用C ++代码实现它?
(输入数量不确定!)
答案 0 :(得分:1)
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int array[1000]; // your heighest input range
vector<int> numbers;
int main() {
string nums; // the numbers in the format "1 2 3 4 10 -20"
getline(cin,nums);
stringstream stream(nums);
int i = 0;
int n;
while(stream >> n){
array[i++] = n;
numbers.push_back(n);
}
// The number of integers in array is i. You can do anything with this number.
// numbers contains the input numbers.
return 0;
}
我在收到 PeterT 的想法后添加了vector
。您可以添加向量以不设置数组的静态大小。
试试这段代码。 stringstream的标头是sstream
。我编写了代码块,我认为这也适用于VC ++编译器。
答案 1 :(得分:1)
正如 PeterT 指出的那样,如果您不知道数组的大小,则必须使用动态内存分配。幸运的是, STL 有一个容器可以为你完成。
您可以std::vector
使用该作业。
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main() {
std::string nums; // the numbers in the format "1 2 3 4 10 -20"
std::getline(std::cin,nums);
std::stringstream stream(nums);
int n;
std::vector<int> vec;
while(stream >> n) {
vec.push_back(n);
}
return 0;
}
(代码基于 Abdulla Al Sun 的回答。)
这是一个O(n)(线性复杂度)解决方案。
如果要将其转换为实际数组,可以执行以下操作:
int array[vec.size()];
std::copy(vec.begin(), vec.end(), array);
另一种方法是通过将输入存储在字符串中并计算令牌来计算用户输入的元素数量。
然后你知道你需要多大的数组。
unsigned int getSize(std::string s) {
unsigned int size = 0;
std::stringstream ss(s);
int in;
while (ss >> in)
++size;
return size;
}
int main() {
std::string nums; // the numbers in the format "1 2 3 4 10 -20"
std::getline(std::cin,nums);
const unsigned int size = getSize(nums);
int array[size];
std::stringstream stream(nums);
int n;
for(unsigned int i = 0; stream >> n && i < size; ++i) {
array[i] = n;
}
return 0;
}
这是一个O(2n)(线性复杂度)解决方案。
我的代码假定编译器允许变量数组大小。如果没有,请使用:
int* array = new int[size];
...
delete[] array;
要使用RAII,请将其包装在如下结构中:
struct DynArr {
int* data;
unsigned int size;
DynArr(const unsigned int size) :
size(size) {
data = new int[size];
}
~DynArr() {
delete[] data;
}
};
答案 2 :(得分:1)
我要窃取阿卜杜拉的代码并进行一些修改。
#include <iostream>
#include <string>
#include <sstream>
#include <cstring.h> // for memcpy
//using namespace std; frowned on. polutes global namespace
//the highest input range is undefined, so this isn't safe
//int array[1000]; // your heighest input range
int main() {
int max=10;
int * array = new int[max];// allow resizing of array by dynamically allocating
std::string nums; // the numbers in the format "1 2 3 4 10 -20"
std::getline(std::cin,nums);
std::stringstream stream(nums);
int i = 0;
while(stream){
if (i==max)
{
int * temp = new int[max*2];// note statistical analysis has found
//1.5 is generally a better multiplier
memcpy(temp, array, max*sizeof(array[0]));
// note do not use memcpy for copying complex data. It is too stupid.
delete array; // release memory of old array
array = temp; // replace old array with new array
max*=2;
}
int n;
stream>>n;
array[i++] = n;
}
// The number of integers in array is i. You can do anything with this number.
delete array; // all done. clean up.
return 0;
}
真正聪明的方法是使用std::vector
。赔率是非常好的,这将是标记的皱眉,所以制作自己的可调整大小的数组类。通过课程,您可以轻松利用RAII并自动进行清理,因此可以安全地使用它。