标签:nbsp col for 迭代 while ++ div 插入 val
//***********9.3.1节练习 9.18************** string temp; deque<string> sdeq;//默认初始化 while (cin >> temp) { sdeq.push_back(temp); } for (auto i = sdeq.cbegin(); i != sdeq.cend(); ++i) cout << *i << endl; //***********9.19************************ list<string> slist; string temp1; while (cin >> temp1) { slist.push_back(temp1); } for (auto i = slist.cbegin(); i != slist.cend(); ++i) cout << *i << endl; //故和上一个练习相比,并无太多改动 //*************9.20*********************** list<int> ilist; for (int i = 0; i != 10; ++i) { ilist.push_back(i); } deque<int> odddeq,evendeq; for (auto i = ilist.cbegin(); i != ilist.cend(); ++i) { if (*i & 1 )//用逻辑位与运算,和%2等价 odddeq.push_back(*i); else evendeq.push_back(*i); } //**************9.21********************** vector<string> svec; string word; auto iter = svec.begin(); while (cin >> word) { iter = svec.insert(iter, word); } for (auto i : svec) cout << i << endl; //此操作相当于一直push_front,从而元素是倒序的 std::system("pause"); return 0;
//*****************9.22练习***********************
//目的是检查,前一半元素中如果有和some_val相等的元素的,就在他前面插入一个2*some_val
//而错误程序是,当插入元素时,原本的迭代器会失效,所以我们要更新这个mid,让他始终在最初始的位置
vector<int> test = { 1, 2, 1, 3, 1, 5, 2, 1, 4, 1 };
for (auto i : test)
{
cout << i << " ";
}
cout << endl;
int some_val = 1;
int newsum = 0;//用来记录插入的新元素的个数
size_t org_size = test.size();
vector<int>::iterator iter = test.begin();
while (iter != test.begin() + org_size / 2 + newsum)// !=后面是新的mid位置
{
if (*iter == some_val)
{
iter = test.insert(iter, 2 * some_val);
iter += 2;
newsum++;
}
else
iter++;
}
for (auto i : test)
{
cout << i << " ";
}
cout << endl;
std::system("pause");
return 0;
标签:nbsp col for 迭代 while ++ div 插入 val
原文地址:https://www.cnblogs.com/Kanna/p/12268433.html