c ++从stdin存储中读取向量和stdout [英] c++ read from stdin store in vector and stdout

查看:223
本文介绍了c ++从stdin存储中读取向量和stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试这个代码,它读取stdin并将其存储在向量和stdout.any任何想法可能是什么问题

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


使用namespace std;

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

字符串缓冲区;
while(!(cin.eof())){
getline(cin,buffer);
cout<<缓冲液< 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:在函数'int main() ':
newcode.cpp:19:error:can not 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:没有匹配'operator!='in'vsi!= vs.std :: vector< _Tp,_Alloc> _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:'unary *'的无效类型参数
[root @ server dev]#
pre>

解决方案

for 循环的初始化部分, int 的新变量 vsi



解决问题的一种方法:

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


i am testing out this code which reads stdin and stores it in vector and stdout..any idea what the problem could be??

#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.

One way to fix the problem:

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

这篇关于c ++从stdin存储中读取向量和stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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