使用解引用操作符*来访问迭代器指向的元素。
C++中的string的下标也是从0开始的。
从一个文本文件中读出数据。包括将字符串转化成整型的函数。
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string str="input.txt",ss;
int a;
stringstream stream;
ifstream infile(str);
if(!infile)
{
cerr<<"error"<<endl;
return -1;
}
getline(infile,ss);
stream<<ss;
stream>>a;
cout<<"有" <<a<<"行"<<endl;
while(getline(infile,ss))
{
cout<<ss<<" length="<<ss.size()<<endl;
}
cin>>a;
}
txt文件为
12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279