2013-08-23 43 views
4

我是在编程竞赛昨天,我们曾在形式如何将cin与可变数量的输入一起使用?

n 
a1 a2 ... an 
m 
b1 b2 ... bm 
... 

,其中第一行说有多少投入有,下一行包含许多输入(和所有的输入是输入阅读整数)。

我知道,如果每一行都有相同数量的输入(比如3),我们可以写类似

while (true) { 
    cin >> a1 >> a2 >> a3; 
    if (end of file) 
     break; 
} 

但是,你怎么做时,每行可有不同数量的投入?

+5

当您需要重复某些操作的次数可变时,您通常需要做什么?你会用什么样的数据结构来存储可变数量的项目? – jamesdlin

+0

@ÖöTiib:我不好,我会解决这个问题。 – Jessica

+0

@ÖöTiib虽然OP没有说清楚,但通常情况下,输入以元素数量的非正值表示。所以,OP应该改变这个条件。 –

回答

7

这里是只使用标准库的简单看法:

#include <vector>  // for vector 
#include <iostream>  // for cout/cin, streamsize 
#include <sstream>  // for istringstream 
#include <algorithm> // for copy, copy_n 
#include <iterator>  // for istream_iterator<>, ostream_iterator<> 
#include <limits>  // for numeric_limits 

int main() 
{ 
    std::vector<std::vector<double>> contents; 

    int number; 
    while (std::cin >> number) 
    { 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // skip eol 
     std::string line; 
     std::getline(std::cin, line); 
     if (std::cin) 
     { 
      contents.emplace_back(number); 
      std::istringstream iss(line); 
      std::copy_n(std::istream_iterator<double>(iss), number, contents.back().begin()); 
     } 
     else 
     { 
      return 255; 
     } 
    } 

    if (!std::cin.eof()) 
     std::cout << "Warning: end of file not reached\n"; 

    for (auto& row : contents) 
    { 
     std::copy(row.begin(), row.end(), std::ostream_iterator<double>(std::cout," ")); 
     std::cout << "\n"; 
    } 
} 

看到它live on Coliru:输入

输出:

1 2 3 4 5 
6 7 8 9 10 11 12 
+1

第二个'std :: cin.ignore()'将忽略m(即7),这就是为什么在第二行输出中只有6个条目存在的原因。 –

+0

@refp哎呀,这是一个马虎:/修正,谢谢你的单挑 – sehe

0

简单,使用for循环和数组。

int a[MAX]; // programming problems usually specify a max size 
for(i=0;i<n;i++) 
cin>>a[i]; 
+0

为什么使用原始数组呢? –

+0

确实,原始数组并不是一个好的编程习惯,但我正在演示如何根据每个案例的元素总数的不同来输入数据。 –

+0

-1。绝对没有理由为什么可能的输入元素的数量应该受数组大小的限制。 –

1

你的算法会是这个样子:

1. read the 'number' of inputs, say n1 
2. set up a loop to read the n1 inputs 
3. check if the user has more inputs to give 
    if YES repeat the steps 1,2 and 3 till all inputs are taken and stored. 
    else move on... 

您可以使用for或while循环和输入存储到一个数组。

希望这会有所帮助!

2

你能做到这样

#include<vector> 
... 
... 
std::vector<sometype> a; 
sometype b; 
std::cin >> b; 
while(std::cin) 
{ 
a.push_back(b); 
std::cin >> b; 
} 

你可以输入任意数量的项目,当你完成的EOF信号发送。

1

因为人们抱怨我怎么叫我的第一个答案“一个简单的拿”,这里使用Boost精神正确的版本:

#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/phoenix.hpp> 

int main() 
{ 
    typedef std::vector<std::vector<double>> data_t; 
    typedef boost::spirit::istream_iterator It; 

    std::cin.unsetf(std::ios::skipws); 
    It first(std::cin), last; 

    bool ok; 
    data_t contents; 

    { 
     using namespace boost::spirit::qi; 
     static rule<It, data_t(),      blank_type, locals<int>> file; 
     static rule<It, std::vector<double>(int number), blank_type>    row; 

     _a_type number; // friendly alias 

     file %= -(omit [int_[number=_1]] > eol > row(number)) % eol; 
     row = repeat(_r1) [ double_ ]; 

     ok = phrase_parse(first, last, file, blank, contents); 
    } 

    if (ok) for (auto& row : contents) 
    { 
     std::copy(row.begin(), row.end(), std::ostream_iterator<double>(std::cout," ")); 
     std::cout << "\n"; 
    } 

    if (first!=last) 
     std::cout << "Warning: end of file not reached, remaining unparsed: '" << std::string(first, last) << "'\n"; 
} 

这显然远远优于

  • 它使用少得多包括行:)
  • 需要大约10倍的编译时间(无优化),另外优化16%
  • 它需要大约5年o ˚F研究的元编程神交它(开玩笑,精神文档/教程是相当确定)
  • 上的严重帐户:这是更灵活

    • 可扩展到解析其他结构元件,更复杂的
    • 可以即时实现语义
    • 将解析楠+/-无穷正确
    • 等。

看到它Live on Coliru以及

1

考虑到您指定的格式,这里我会做什么。

for (int n; std::cin >> n;) 
{ 
    if (n == 0)  // Test for end of input 
     break; 

    for (int i = 0; i != n; ++i) 
    { 
     int x; 
     std::cin >> x; 
     if (!std::cin) 
      break; 

     // Valid input x. Now do something with x like 
     // v.push_back(x) where v is some vector of ints 
    } 
} 

// Did we succeed? 
if (!std::cin) 
{ 
    // Something went bad. 
    std::cerr << "Error reading input" << std::endl; 

    return EXIT_FAILURE; 
} 
0

一个简单的解决方案,使用数组和动态内存分配来获取预定义类型的可变数量的输入。

#include<iostream> 
using namespace std; 

int main(){ 
int n; 
cout<<"Enter the number of elements"<<endl; 
cin>>n; 
int *c=new int[n]; 
for(int k=0;k<n;k++) 
    cin>>c[k]; 
} 
相关问题