51. 如何从一个 list<int>初始化一个 vector<double>?从一个 vector<int>又
该如何创建?编写代码验证你的答案。
#include<iostream>
#include<vector>
#include<list>
using namespace std;
void printVector(vector<double> &v)
{
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
//采用迭代器范围初始化
list<int> L{ 1,2,3,4,5 };
vector<double> V1(L.begin(), L.end());
printVector(V1);
vector<int> V2{ 6,7,8,9,10 };
vector<double> V3(V2.begin(), V2.end());
printVector(V3);
return 0;
}
52. 编程程序,将一个 list 中的 char*指针(指向 C 风格字符串)元素赋值给
一个 vector 中的 string。
#include<iostream>
#include<list>
#include<vector>
#include<string>
using namespace std;
void print(vector<string>& v)
{
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<const char *> L={ "I"," LOVE ","YOU" };
vector<string> V;
V.assign(L.begin(), L.end());
cout << "capacity:" << V.capacity() << " size:" << V.size() << endl;
print(V);
return 0;
}
53. 编写程序,从标准输入读取 string 序列,存入一个 deque 中。编写一个循 环,用迭代器打印 deque 中的元素。
#include<iostream>
#include<string>
#include<deque>
using namespace std;
void print(deque<string>& d)
{
for (auto it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
string s;
deque<string> d;
while (cin >> s)
{
d.push_back(s);
}
print(d);
}
54. 编写程序,从一个 list拷贝元素到两个 deque 中,其中值为偶数的所 有元素都拷贝到一个 deque 中,而奇数元素都拷贝到另一个 deque 中。
#include<iostream>
#include<list>
#include<deque>
using namespace std;
void print(deque<int>& d)
{
for (auto it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<int> L{ 10,2,3,9,5,6,7,8,4,1 };
deque<int> d1, d2;
for (auto it = L.begin(); it != L.end(); it++)
{
if (*it % 2 == 0)
{
d1.push_back(*it);
}
else
{
d2.push_back(*it);
}
}
print(d1);
print(d2);
}
55. 假定你希望每次读取一个字符存入一个 std::string 中,而且知道最少需要 读取 100 个字符,应该如何提高程序的性能?
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
char c;
s.reserve(100);
while (cin >> c)
{
s += c;
}
cout << s << endl;
}
56. 编写一个函数,接受一个表示名字的 std::string 参数和两个分别表示前缀 (如“Mr.”或“Ms.”)和后缀(如“Jr.”“III”)的字符串。使用迭代器及 insert 和 append 函数将前缀和后缀加到给定的名字中,生成新的 string 并返 回。
#include<iostream>
#include<string>
using namespace std;
//string &insert(int p0, const char *s);
//1、在p0位置插入字符串s
//string &insert(int p0, const char *s, int n);
//2、在p0位置插入字符串s的前n个字符
//string &insert(int p0,const string &s);
//3、在p0位置插入字符串s
//string &insert(int p0,const string &s, int pos, int n);
//4、在p0位置插入字符串s从pos开始的连续n个字符
//string &insert(int p0, int n, char c);
//5、在p0处插入n个字符c
//iterator insert(iterator it, char c);
//6、在it处插入字符c,返回插入后迭代器的位置
//void insert(iterator it, const_iterator first, const_iteratorlast);
//7、在it处插入从first开始至last-1的所有字符
//void insert(iterator it, int n, char c);
//8、在it处插入n个字符c
int main()
{
//前缀,后缀
string prefix="Ms.", suffix="III";
string s="Right";
s.insert(s.begin(), prefix.begin(), prefix.end());
s.append(suffix.begin(), suffix.end());
cout << s << endl;
}
57. 定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们) 的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
//添加家庭的姓
void add_family(map<string, vector<string>>& families, const string& family)
{
families[family];
}
void add_child(map<string, vector<string>>& families, const string& family, const string& child)
{
families[family].push_back(child);
}
int main()
{
map<string, vector<string>> families;
add_family(families, "Li");
add_child(families, "Li", "Si");
add_child(families, "Li", "Wu");
add_family(families, "Zhang");
add_child(families, "Zhang", "san");
add_family(families, "Wang");
for (auto a : families)
{
cout << a.first << " family: ";
for (auto b : a.second)
{
cout << b << " ";
}
cout << endl;
}
}
//map<int, string> m;
//map插入方式:
//m.insert(pair<int, string>(1, "student"));
//m.insert(map<int, string>::value_type(1, "student"));
//m.insert(make_pair(1, "student"));
//m[1] = "student";
//m.insert({ 1, "student });
58. 编写一个程序,在一个 vector 而不是一个 set 中保存不重复的单词。使用 set 的优点是什么?
#include<iostream>
#include<algorithm>
#include<fstream>
#include<string>
#include<vector>
#include<set>
using namespace std;
/*
vector保存不重复的单词,需要用find查找新单词是否已在vector中,
而set会自动检测。vector可以保持输入的单词顺序,
而set在输入时,会自动排序
*/
string& trans(string& s)
{
for (int p = 0; p < s.size(); p++)
{
if (s[p] >= 'A' && s[p] <= 'Z')
{
s[p] -= ('A' - 'a');
}
else if (s[p] == ',' || s[p] == '.')
{
s.erase(p, 1);
}
}
return s;
}
//set
//int main(int argc, char* argv[])
//{
// ifstream in(argv[1]);
// if (!in)
// {
// cout << "打开输入文件失败" << endl;
// exit(1);
// }
// set<string> unique_word;
// string word;
// while (in >> word)
// {
// trans(word);
// if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())
// {
// unique_word.insert(word);
// }
// }
// for (auto& w : unique_word)
// {
// cout << w << " ";
// }
// cout << endl;
// return 0;
//}
//vector
int main(int argc, char* argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
vector<string> unique_word;
string word;
while (in >> word)
{
trans(word);
if (find(unique_word.begin(), unique_word.end(), word )== unique_word.end())
{
unique_word.push_back(word);
}
}
for (auto& w : unique_word)
{
cout << w << " ";
}
cout << endl;
return 0;
}
59. 可以用什么类型来对一个 map 进行下标操作?下标运算符返回的类型时什 么?请给出一个具体例子,即定义一个 map,然后写出一个可以用来对 map 进行 下标操作的类型以及下标运算符将会返回的类型。
/*
对map进行下标操作,应使用其key_type,即关键字的类型
而下标操作返回的类型是mapped_type,即关键字关联的值的类型
例如:
map<string,int>
下标操作的类型:string
下标运算符将会返回的类型:int
*/
60. 用冒泡法对 10 个整数排序。(用 STL 的 vector 容器实现)
#include<iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v)
{
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void bubbleSort(vector<int>& v)
{
for (auto i = v.begin(), j = v.end(); i < j; j--)
{
for (auto k = i + 1; k < j; k++)
{
if (*k < *(k - 1)) swap(*k, *(k - 1));
}
}
}
int main()
{
vector<int> v = { 10,6,3,9,5,2,7,8,4,1 };
cout << "排序前:" << endl;
printVector(v);
bubbleSort(v);
cout << "排序后:" << endl;
printVector(v);
return 0;
}