8.7:
是考察ofstream操作类似于:
ifstream is("test_2.txt");
ofstream os("test_w_2.txt");
string tmpStr;
while (is >> tmpStr && !is.eof()) {
cout << tmpStr << ends;
os << tmpStr << endl;
}
8.8
os打开文件时候使用app追加模式
8.9
istringstream& read(istringstream& is){
string i;
while (is>> i)
{
cout << i ;
}
is.clear();
return is;
}
8.10
int main() {
ifstream is("test_2.txt");
string tmpStr;
vector<string > strVec;
while (getline(is, tmpStr)){
strVec.push_back(tmpStr);
}
string word;
istringstream iss;
for(auto strAuto : strVec){
istringstream iss(strAuto);
while (iss >> word) {
cout << word<<ends;
}
}
for (;;);
return 0;
}
8.11:
string line,word;
vector<PersonInfo> people;
istringstream record;
while(getline(is,line)){
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while(record >> word)
info.phones.push_back(word);
people.push_back(info);
}
8.12
每个人情况不一样,甚至没有手机号码,无法针对所有人进行统一匹配
8.13
ifstream is("test_2.txt");
string line,word;
vector<PersonInfo> people;
while(getline(is,line)){
PersonInfo info;
istringstream record(line);
record >> info.name;
while(record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for(const auto &entry:people){
ostringstream formatted, badNums;
for(const auto &nums:entry.phones){
if(!valid(nums)){
badNums << " "<<nums;
}else{
formatted << " " format(nums);
}
}
if (badNums.str().empty()) {
cout << entry.name << " " << formatted.str() << endl;
}else{
cerr << "input error: " << entry.name << "invalid numbers(s)" << badNums.str() << endl;
}
}
8.14:
entry和nums都是数据源的值,我们无需改变他们所以加上const,加上引用后可以使传递更快,加快程序运行效率
9.1
a.vector
b.deque
c.list
9.2
list<deque<int> > dal;
9.3
它们只想同一个容器中的元素,或者是容器最后一个元素之后的位置
我们可以通过反复递增begin来到大end,换句话说,end不在begin之前
9.4
bool findNumInVec(vector<int > vec, int theKeyNum){
for (vector<int >::iterator it = vec.begin(); it != vec.end(); it++) {
if (*it == theKeyNum)
return true;
}
return false;
}
9.5
int findNumInVec(vector<int > vec, int theKeyNum){
for (vector<int >::iterator it = vec.begin(); it != vec.end(); it++) {
if (*it == theKeyNum)
return *it;
}
return theKeyNum+1;
}
9.6
iter1没有改变指针的位置,会导致循环不会跳出
9.7
vector<int>::size_type
9.8
list<string>::iterator
list<string>::const_iterator
9.9
一个可修改一个不可修改
9.10
it1 : vector<int>::iterator
it2:
it3:
it4:
vector<int>::const_iterator
vector<int>:::iterator
vector<int>:::iterator