简体   繁体   English

从向量和标准输出中的stdin存储读取的c ++

[英]c++ read from stdin store in vector and stdout

i am testing out this code which reads stdin and stores it in vector and stdout..any idea what the problem could be?? 我正在测试此代码,该代码读取stdin并将其存储在vector和stdout ..任何想法中可能是什么问题?

#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main() {
  vector<string> vs;
  vector<string>::iterator vsi;

  string buffer;
  while (!(cin.eof())) {
    getline(cin, buffer);
    cout << buffer << endl;
    vs.push_back(buffer);
  };

  for (int count=1 , vsi = vs.begin(); vsi != vs.end(); vsi++,count++){
    cout << "string" << count <<"="<< *vsi << endl;
  }

  return 0;
}



[root@server dev]# g++ -o newcode newcode.cpp 
newcode.cpp: In function ‘int main()’:
newcode.cpp:19: error: cannot convert ‘__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ to ‘int’ in initialization
newcode.cpp:19: error: no match for ‘operator!=’ in ‘vsi != vs.std::vector<_Tp, _Alloc>::end [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]()’
newcode.cpp:20: error: invalid type argument of ‘unary *’
[root@server dev]# 

In the initialization part of the for loop, you declare a new variable vsi whose type is int . for循环的初始化部分,您声明一个新变量vsi其类型为int

One way to fix the problem: 解决问题的一种方法:

vsi = vs.begin();
for (int count=1; vsi != vs.end(); ...

Problem is on this line: 问题在这条线上:

for (int count=1 , vsi = vs.begin(); vsi != vs.end(); vsi++,count++)

You define two int variables: count and vsi . 您定义了两个int变量: countvsi Then, you try to assign the second with vs.begin() . 然后,尝试使用vs.begin()分配第二个。 This is what the compiler complains about. 这就是编译器所抱怨的。

The problem is that vs.begin() does not return an int, and you declared vsi as an integer. 问题在于vs.begin()不返回int,并且您将vsi声明为整数。

Easy fix: 轻松解决:

for (int count=0;count < vs.size(); ++count){
  cout << "string" << (count+1) <<"="<< vs[count] << endl;
}

Notes: 笔记:

  • prefer ++count to count++ 更喜欢++countcount++
    Though it makes not difference in this case there are situations where it does. 尽管在这种情况下没有区别,但在某些情况下确实有区别。
    So it is a good habit to get into. 因此,这是一个好习惯。
    See: Performance difference between ++iterator and iterator++? 请参阅: ++ iterator和Iterator ++之间的性能差异?

  • while (!(cin.eof())) is practically always wrong (in all languages). while (!(cin.eof()))实际上总是错误的(在所有语言中)。
    The 'eof flag' is not set to true until after you read past the eof. 直到您读完eof之后,“ eof标志”才设置为true。
    The last successful read reads up-to (but not past) the eof. 最后一次成功的读取最多读取(但不超过)eof。 So you enter the loop a final time and the read will fail yet you still push back a value into the vector. 因此,您最后一次进入循环,读取将失败,但是您仍然将值推回向量中。

    • In Some situations this can cause an infinite loop. 在某些情况下,这可能会导致无限循环。
      If there is another type of failure on read you will never reach the eof 如果读取时出现其他类型的故障,您将永远无法达到目标
      (eg cin >> x; Where x is an int may fail if input is not an integer) (例如,cin >> x;如果输入不是整数,则x为int可能会失败)
      See: c++ reading undefined number of lines with eof() 请参阅: c ++使用eof()读取未定义的行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C ++中的stdin读取 - Read from stdin in C++ 如何在C ++中运行应用程序并写入其stdin并在Windows中从其stdout读取 - how to run an application in c++ and write to its stdin and read from its stdout in windows 编辑在c / c ++中从stdin打印在stdout上的文本 - Edit text which printed on stdout from stdin in c/c++ C / C ++服务器,通过stdin / stdout与客户端进行通信(在stdin上阻塞,直到读取了多个字节) - C/C++ server, communicating by stdin/stdout with clients (blocking on stdin until a number of bytes has been read) 使用stdout stdin将数组从C ++ exe传递给Python - Pass array from C++ exe to Python using stdout stdin C ++在stdin上写入数据并从stdout获取输出 - C++ write data on stdin and get output from stdout 异步将stdout / stdin从嵌入式python重定向到c ++? - Asynchronously redirect stdout/stdin from embedded python to c++? c++ 帮助从标准输入和标准输出读取。 还有运算符重载 - c++ Help with reading in from stdin and to stdout . Also Operator overloading 是否有类似的方法从stdin到vector读取整数对 <pair<int,int> &gt;在C ++中 - Is there a similar way to read integer pairs from stdin to vector<pair<int,int> > in C++ 用C ++将stdin的内容读入字符串或向量的最快方法 - Fastest way in c++ to read contents of stdin into a string or vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM