如何读取用户输入的逗号分隔整数? [英] How to read in user entered comma separated integers?

查看:389
本文介绍了如何读取用户输入的逗号分隔整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,提示用户:

I'm writing a program that prompts the user for:


  1. 数组大小

  2. 要放入数组中的值

第一部分很好,我创建了一个动态分配的数组(必需)并使其大小用户想要的。

First part is fine, I create a dynamically allocated array (required) and make it the size the user wants.

我被困在下一部分。预计用户将输入一系列用逗号分隔的整数,例如:1,2,3,4,5

I'm stuck on the next part. The user is expected to enter in a series of ints separated by commas such as: 1,2,3,4,5

我如何输入这些整数并放入他们到我动态分配的数组?我读到默认情况下cin接受以空格分隔的整数,我可以将其更改为逗号吗?

How do I take in those ints and put them into my dynamically allocated array? I read that by default cin takes in integers separated by whitespace, can I change this to commas?

请以最简单的方式进行说明,我是编程的初学者(抱歉!)

Please explain in the simplest manner possible, I am a beginner to programming (sorry!)

编辑:TY对于所有答案都非常重要。问题是我们还没有覆盖向量...是否有一种方法仅使用我拥有的动态分配数组?

TY so much for all the answers. Problem is we haven't covered vectors...is there a method only using the dynamically allocated array I have?

到目前为止,我的函数看起来像这样。我在main中做了一个默认数组。我计划将其传递给此函数,创建新数组,填充它,并更新指针以指向新数组。

so far my function looks like this. I made a default array in main. I plan to pass it to this function, make the new array, fill it, and update the pointer to point to the new array.

int *fill (int *&array, int *limit) {

cout << "What is the desired array size?: ";                                  
while ( !(cin >> *limit) || *limit < 0 ) {
    cout << "  Invalid entry. Please enter a positive integer: ";
    cin.clear();
    cin.ignore (1000, 10);
}

int *newarr;                                                                            
newarr = new int[*limit]
    //I'm stuck here
}


推荐答案

所有现有的答案都很好,但是所有答案都是特定于您的特定任务的。因此,我编写了一些通用的代码,允许以标准方式输入逗号分隔的值:

All of the existing answers are excellent, but all are specific to your particular task. Ergo, I wrote a general touch of code that allows input of comma separated values in a standard way:

template<class T, char sep=','>
struct comma_sep { //type used for temporary input
    T t; //where data is temporarily read to
    operator const T&() const {return t;} //acts like an int in most cases
};
template<class T, char sep>
std::istream& operator>>(std::istream& in, comma_sep<T,sep>& t) 
{
    if (!(in >> t.t)) //if we failed to read the int
        return in; //return failure state
    if (in.peek()==sep) //if next character is a comma
        in.ignore(); //extract it from the stream and we're done
    else //if the next character is anything else
        in.clear(); //clear the EOF state, read was successful
    return in; //return 
}

示例用法 http://coliru.stacked-crooked.com/a/a345232cd5381bd2

typedef std::istream_iterator<comma_sep<int>> istrit; //iterators from the stream
std::vector<int> vec{istrit(in), istrit()}; //construct the vector from two iterators

由于您是初学者,因此这段代码可能太多了现在为您服务,但我想我会出于完整性考虑而发布。

Since you're a beginner, this code might be too much for you now, but I figured I'd post this for completeness.

这篇关于如何读取用户输入的逗号分隔整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆