C++primer习题3.14 vector<string>读写字符

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void get_upper(string &s)
{
	for(size_t i = 0; i != s.size(); ++i)
	{
		s[i] = toupper(s[i]);
	}
}
int main()
{
	ifstream infile("E:\\test.txt");
	if (!infile)
	{
		cerr << "error" << endl;
	}
	vector<string> svec;
	string s;
    while (infile)
    {
		infile >> s;
		svec.push_back(s);
    }
	for(size_t i = 0; i != svec.size(); ++i)
	{
		get_upper(svec[i]);
		cout << svec[i] << " ";
		if ((i+1)%8==0)
		{
			cout << endl;
		}
	}
}

使用迭代器:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void get_upper(string &s)
{
	for(size_t i = 0; i != s.size(); ++i)
	{
		s[i] = toupper(s[i]);
	}
}
int main()
{
	ifstream infile("E:\\test.txt");
	if (!infile)
	{
		cerr << "error" << endl;
	}
	vector<string> svec;
	string s;
    while (infile)
    {
		infile >> s;
		svec.push_back(s);
    }
	int i = 1;
	for(vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
	{
		get_upper(*iter);
		cout << *iter << " ";
		if (i++%8==0)
		{
			cout << endl;
		}
	}
}

原文地址:https://www.cnblogs.com/xiangshancuizhu/p/2075758.html