C++ 如何从键盘上读取C++;?

C++ 如何从键盘上读取C++;?,c++,cin,C++,Cin,我需要从键盘上读取一个可变数量的整数,这样我就可以使用它们中的每一个 首先我想我可以用像这样的东西 int myinteger; for (int i=0; i<MAX_NUMBER_OF_INTEGERS; i++){ cin >> myinteger; //What I want to do with that Integer } int-myinteger; 对于(int i=0;i>myinteger; //我想用这个整数做什么

我需要从键盘上读取一个可变数量的整数,这样我就可以使用它们中的每一个

首先我想我可以用像这样的东西

    int myinteger;
    for (int i=0; i<MAX_NUMBER_OF_INTEGERS; i++){
    cin >> myinteger;
    //What I want to do with that Integer
    }
int-myinteger;
对于(int i=0;i>myinteger;
//我想用这个整数做什么
}
但后来我意识到,如果MAX\u NUMBERS\u OF\u INTEGERS=10,我必须写10个整数。但我想要的是,我可以通过“1 2 3”“1 2 3 4”(例如),而不必写10个整数

std::vector<int> read_ints;
int _temp;
for(;;)
{        
    cin >>_temp;
    if(!cin.good()) {
        break;
    }
    else {
        read_ints.push_back(_temp);
    }
}
setw限制输入字符数,但必须包含iomanip头

如果要访问每个数字,请使用以下函数将int转换为int的向量:

vector <int> integerToArray(int x)
{
    vector <int> resultArray;
    while (true)
    {
        resultArray.insert(resultArray.begin(), x%10);
        x /= 10;
        if(x == 0)
            return resultArray;
    }
 }
向量整数数组(int x)
{
矢量结果射线;
while(true)
{
resultArray.insert(resultArray.begin(),x%10);
x/=10;
如果(x==0)
返回结果数组;
}
}
然后,您可以使用索引访问每个数字,例如第一个数字

 vectory<int> resultArray = integerToArray(my_int);
 int digit = resultArray[0];
vectory resultArray=integerToArray(my_int);
整数位数=结果数组[0];

setw限制输入字符数,但必须包含iomanip头

如果要访问每个数字,请使用以下函数将int转换为int的向量:

vector <int> integerToArray(int x)
{
    vector <int> resultArray;
    while (true)
    {
        resultArray.insert(resultArray.begin(), x%10);
        x /= 10;
        if(x == 0)
            return resultArray;
    }
 }
向量整数数组(int x)
{
矢量结果射线;
while(true)
{
resultArray.insert(resultArray.begin(),x%10);
x/=10;
如果(x==0)
返回结果数组;
}
}
然后,您可以使用索引访问每个数字,例如第一个数字

 vectory<int> resultArray = integerToArray(my_int);
 int digit = resultArray[0];
vectory resultArray=integerToArray(my_int);
整数位数=结果数组[0];

从一行中读取所有数字的一种方法是使用std::getline()将该行转换为字符串,然后在循环中使用istringstream

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    std::string line; 
    std::getline (std::cin,line); 
    std::istringstream iss(line);

    int myInt;

    for(int i=0;(iss >> myInt) && (i < MAX_NUMBER_OF_INTEGERS);++i ) {
        std::cout << myInt << ' ';
    }

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
std::字符串行;
std::getline(std::cin,line);
标准::istringstream iss(线);
int-myInt;
对于(int i=0;(iss>>myInt)和&(istd::cout从一行中读取所有数字的一种方法是使用std::getline()将该行转换为字符串,然后在循环中使用istringstream

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    std::string line; 
    std::getline (std::cin,line); 
    std::istringstream iss(line);

    int myInt;

    for(int i=0;(iss >> myInt) && (i < MAX_NUMBER_OF_INTEGERS);++i ) {
        std::cout << myInt << ' ';
    }

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
std::字符串行;
std::getline(std::cin,line);
标准::istringstream iss(线);
int-myInt;
对于(int i=0;(iss>>myInt)和&(istd::cout自从被问到这个问题以来,这个问题似乎有了一点变化,并且给出了一个很好的答案。这只是为了回答新的问题

#include <iostream>
#include <sstream>
#include <vector>

const int MAX_NUMBERS_OF_INTEGERS = 10;

int main() {
    std::string line;
    std::cout << "Enter at most " << MAX_NUMBERS_OF_INTEGERS << " ints, separated by spaces: ";
    std::getline(std::cin, line);

    // create a string stream of the line you entered
    std::stringstream ss(line);

    // create a container for storing the ints
    std::vector<int> myInts;

    // a temporary to extract ints from the string stream
    int myInteger;

    // extract at most MAX_NUMBERS_OF_INTEGERS ints from the string stream
    // and store them in the container
    while(myInts.size()<MAX_NUMBERS_OF_INTEGERS && ss>>myInteger) myInts.push_back(myInteger);

    std::cout << "Extracted " << myInts.size() << " integer(s)\n";

    // loop through the container and print all extracted ints.
    for(int i : myInts) {
        std::cout << i << "\n";
    }
    // ... or access a certain int by index
    if(myInts.size() > 2)
        std::cout << "The third int was: " << myInts[2] << "\n";

}
#包括
#包括
#包括
常量int最大整数个数=10;
int main(){
std::字符串行;

std::cout自从被问到这个问题以来,这个问题似乎有了一点变化,并且给出了一个很好的答案。这只是为了回答新的问题

#include <iostream>
#include <sstream>
#include <vector>

const int MAX_NUMBERS_OF_INTEGERS = 10;

int main() {
    std::string line;
    std::cout << "Enter at most " << MAX_NUMBERS_OF_INTEGERS << " ints, separated by spaces: ";
    std::getline(std::cin, line);

    // create a string stream of the line you entered
    std::stringstream ss(line);

    // create a container for storing the ints
    std::vector<int> myInts;

    // a temporary to extract ints from the string stream
    int myInteger;

    // extract at most MAX_NUMBERS_OF_INTEGERS ints from the string stream
    // and store them in the container
    while(myInts.size()<MAX_NUMBERS_OF_INTEGERS && ss>>myInteger) myInts.push_back(myInteger);

    std::cout << "Extracted " << myInts.size() << " integer(s)\n";

    // loop through the container and print all extracted ints.
    for(int i : myInts) {
        std::cout << i << "\n";
    }
    // ... or access a certain int by index
    if(myInts.size() > 2)
        std::cout << "The third int was: " << myInts[2] << "\n";

}
#包括
#包括
#包括
常量int最大整数个数=10;
int main(){
std::字符串行;

std::cout最简单的方法是将一个值定义为输入结束标记。可能是,假设-1不是与您的问题相关的有效整数,则类似于“Enter-1 to finish”。或者,您可以读取整数直到输入失败,例如,
for(int-value;cin>>value;){/*store value*/}
。您可以使用while(cin>>myinteger){//对myinteger做点什么}并让用户在linux上按
ctrl-d
,或在windows上按
ctrl-z
,以结束输入。谢谢你们两位,但我认为这对我不起作用,因为“用户”是一个在线法官,检查我的程序是否有效并输出正确的结果results@Uwunt然后,您必须修改您的问题,以准确说出您想要的内容。我们三个人都认为您不想要固定的最大值,因此我们为您提供了一些方法,允许用户输入他们想要的数字,并发出完成的信号。@uunt老实说目前还不清楚。你想将最大值传递到哪里?作为函数参数,还是希望用户通过输入输入输入最大值?使用我的解决方案,你可以通过键入非数字字符来中止输入。因此,你不必写入最大值。如果这不是你想要的,你必须更精确地输入最大值简单的方法是将一个值定义为输入结束标记。可能类似于“Enter-1 to finish”,假设-1不是与您的问题相关的有效整数。或者,您可以读取整数直到输入失败,例如,
for(int-value;cin>>value;){/*store value*/}
。您可以使用while(cin>>myinteger){//do some with myinteger}并让用户在linux上按
ctrl-d
,或在windows上按
ctrl-z
,以结束输入。谢谢你们两位,但我认为这对我不起作用,因为“用户”是一个在线法官,检查我的程序是否有效并输出正确的结果results@Uwunt然后,您必须修改您的问题,以准确说出您想要的内容。我们三个人都认为您不想要固定的最大值,因此我们为您提供了一些方法,允许用户输入他们想要的数字,并发出完成的信号。@uunt老实说现在还不清楚。你想把最大值传递到哪里?作为一个函数参数,还是希望用户通过输入输入最大值?在我的解决方案中,你可以通过输入一个非数字字符来中止输入。所以你不必写那么多的最大值。如果这不是你想要的,你必须更精确。我认为dOe不适用于我,因为我的程序应该能够读取3个数字,5个数字,…。定义了最大值并对每个数字进行操作。我以为你的目标是不指定最大值?至少这是我从你的问题中读到的。如果不是,请澄清你的具体目标。这看起来不错,只有一个问题。我如何使用eac输入h号?@Uw