意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

13 标准模板库STL【C++】

来源:恒创科技 编辑:恒创科技编辑部
2024-01-28 21:10:59


13 标准模板库STL​​13-​​​​判断题​​​​单选题​​​​填空题​​​​程序填空题​​​​函数题​​​​7-1 .查找电话号码​​​​7-2 姓名排序​​​​7-3 Score Processing​​​​13+​​​​编程题​​​​7-1 查找成绩并折算后输出​​​​7-2 电话号码同步​​​​7-3 姓名排序​​​​STL测试​​13-判断题

1-1 可以通过下标随机访问向量vector中的元素。T

1-2 当向量对象的内存用完之后,就会产生越界错误。F


13 标准模板库STL【C++】

单选题

2-1 若有下面的语句:

vector<int> v;
for (int i = 0; i < 4; i++)
v.push_back(i + 1);
cout << v.size() << endl;


执行后程序的输出结果是 D

A. 1
B. 2
C. 3
D. 4

2-2 设有定义 vector< string > v(10);执行下列哪条语句时会调用构造函数? C

A. v[0] += “abc”;
B. v[0] = “2018”;
C. v.push_back(“ZUCC”);
D. cout << (v[1] == “def”);

2-3 .设有如下代码段:

std::map<char *, int> m;
const int MAX_SIZE = 100;
int main() {
char str[MAX_SIZE];
for (int i = 0; i < 10; i++) {
std::cin >> str;
m[str] = i;
}
std::cout << m.size() << std::endl;
}

读入10个字符串,则输出的 m.size() 为 B

A. 0
B. 1
C. 10

填空题

4-1(6分)
A container holds a sequence of objects.Containers can be categorized like this:
• Sequence containers provide access to (half-open) ​​​sequence ​​​ of elements.
• Associative containers provide ​​​value​​​ lookup based on a ​​key​​.

4-2(8分)
An iterator is akin to a ​​​pointer​​​ in that it provides operations for ​​dereferencing​​​access (e.g., ∗ for dereferencing) and for moving to point to a new element (e.g., ++ for moving to the ​​next​​ element). A sequence is defined by a pair of iterators defining a half-open range [begin:end):

13 标准模板库STL【C++】_c++

That is, begin points to the ​​first​​ element of the sequence, and end points to the one-beyond-the-last element of the sequence. Never read from or write to ∗end. Note that the empty sequence has begin==end; that is, [p:p) is the empty sequence for any iterator p.

程序填空题

5-1.查找雇员信息(关联容器map)
阅读程序并填空。
#include < iostream >
#include < cstdlib >
#include < map >
#include < string >
using namespace std;

class employee{
public:
employee(string name,string phoneNumber,string address){
this->name=name;
this->phoneNumber=phoneNumber;
this->address=address;
}
string name;
string phoneNumber;
string address;
};
int main()
{
map<int,employee*> employeeMap;
typedef pair<int,employee*>employeePair;
for(int employIndex=1001;employIndex<=1003;employIndex++){
char temp[10]; //临时存储单元
sprintf(temp,“%d”,employIndex);//将转化为字符串存储在temp中
string tmp( temp ); // 通过temp构造string对象
employee* p= new employee(2分)(“employee-”+tmp,“85523927-”+tmp, “address-”+tmp);
employeeMap.insert(2分)(employeePair(employIndex,p));//将员工编号和员工信息插入到employeeMap对象中
}
int employeeNo=0;
cout<<“请输入员工编号:”;
cin>>employeeNo; // 从标准输入获得员工号
map<int, employee*>(2分) ::iterator it;
it=employeeMap.find(employeeNo); // 根据员工编号查找员工信息
if(it==employeeMap.end()(2分)){
cout<<“该员工编号不存在!”<<endl;
return -1;
}
cout<<“你所查询的员工编号为:”<<it ->first<<endl;
cout<<“该员工姓名:”<<it ->second->name<<endl;
cout<<“该员工电话:”<<it -> second -> phonrNumber(2分)<<endl;
cout<<“该员工地址:”<<it ->second->address<<endl;
return 0;
}
5-2 文件目录树
阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

【说明】现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下所示

13 标准模板库STL【C++】_开发语言_02


【C++代码】

#include

#include

#include

using namespace std;

class AbstractFile {
protected :
string name; // 文件或目录名称
public:
void printName(){cout << name;} // 打印文件或目录名称
virtual void addChild(AbstractFile *file)=0; // 给一个目录增加子目录或文件
virtual void removeChild(AbstractFile file)=0;// 删除一个目录的子目录或文件
virtual list<AbstractFile
> *getChildren()=0;// 获得一个目录的子目录或文件
};
class File : public AbstractFile {
public :
File(string name) {
this->name = name; }
void addChild(AbstractFile *file) { return ; }
void removeChild(AbstractFile *file) { return ; }

list<AbstractFile*>* getChildren() { return NULL; }
};
class Folder :public AbstractFile {
private :
list <AbstractFile*> childList; // 存储子目录或文件
public :
Folder(string name) { this->name= name; }
void addChild(AbstractFile *file) { childList.push_back(file); }
void removeChild(AbstractFile file) { childList.remove(file);}
list<AbstractFile
> *getChildren() { return &childList ; }
};

int main( ) {
// 构造一个树形的文件/目录结构
AbstractFile *rootFolder = new Folder(“c:\”);
AbstractFile *compositeFolder = new Folder(“composite”);
AbstractFile *windowsFolder = new Folder(“windows”);
AbstractFile *file = new File(“TestComposite.java”);
rootFolder->addChild(compositeFolder);
rootFolder->addChild(windowsFolder);
compositeFolder->addChild(file);
return 0;
}

函数题7-1 .查找电话号码
7-1 .查找电话号码
文件phonebook1.txt中有若干联系人的姓名和电话号码。
高富帅 13312342222
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found.
由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。
输入格式:
高富帅 13312342222
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname (表示结束)
唐三藏 (需要查找此人的电话号码)
输出格式:
13612346666 (输出对应的电话号码)
输入样例:
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精

输出样例:
Not found.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int main() {
map<string, string> m;
string a;
string b;
while(1) {
cin >> a;
if (a == "noname")
break;
cin >> b;
m.insert(pair<string, string>(a, b));
}
map<string, string> :: iterator p;
string s;
cin >> s;
p = m.find(s);
if (p != m.end())
cout << p -> second << endl;
else
cout << "Not found." << endl;
return 0;
}
7-2 姓名排序
7-2 姓名排序
从指定文本文件中读入若干学生姓名并按照拼音顺序排序后输出。
由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名,当输入的单词为end时,表示文件结束。将按照姓名拼音顺序排序后输出。
输入格式:
张三
李四
王五
马六
陈七
孙悟空
end
输出格式:
陈七 李四 马六 孙悟空 王五 张三
输入样例:
白富美
孙悟空
唐三藏
猪悟能
沙悟净
end

输出样例:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main() {
string name;
set<string> m;
getline(cin, name);
while (name != "end") {
m.insert(name);
getline(cin, name);
}
set<string> :: iterator p = m.begin();
while (p != m.end())
cout << *p++ << " ";
}
7-3 Score Processing
7-3 Score Processing
Write a program to process students score data.

The input of your program has lines of text, in one of the two formats:

Student's name and student id, as <student id>, <name>, and
Score for one student of one course, as <student id>, <course name>, <marks>.
Example of the two formats are:

3190101234, Zhang San
3190101111, Linear Algebra, 89.5
Comma is used as the seperator of each field, and will never be in any of the fields. Notice that there are more than one word for name of the person and name of the course. To make your code easier, the score can be treated as double.

The number of the students and the number of the courses are not known at the beginning. The number of lines are not known at the beginning either. The lines of different format appear in no order. One student may not get enrolled in every course.

Your program should read every line in and print out a table of summary in .csv format.

The first line of the output is the table head, consists fields like this:

student id, name, <course name 1>, <course name 2>, ..., average
where the course names are all the courses read, in alphabet order. There should be one space after each comma.

Then each line of the output is data for one student, in the ascended order of their student id, with score of each course, like:

3190101234, Zhang San, 85.0, , 89.5, , , 87.3
For the course that hasn't been enrolled, leave a blank before the comma, and should not get included in the average. The average has one decimal place. There should be one space after each comma.

And the last line of the output is a summary line for average score of every course, like:

, , 76.2, 87.4, , , 76.8
All the number output, including the averages have one decimal place.

Input Format
As described in the text above.

Output Format
As described in the text above.
The standard output is generated by a program compiled by gcc, that the round of the first decimal place is in the "gcc way".

Sample Input
3180111435, Operating System, 34.5
3180111430, Linear Algebra, 80
3180111435, Jessie Zhao
3180111430, Zhiwen Yang
3180111430, Computer Architecture, 46.5
3180111434, Linear Algebra, 61.5
3180111434, Anna Teng
Sample Output
student id, name, Computer Architecture, Linear Algebra, Operating System, average
3180111430, Zhiwen Yang, 46.5, 80.0, , 63.2
3180111434, Anna Teng, , 61.5, , 61.5
3180111435, Jessie Zhao, , , 34.5, 34.5
, , 46.5, 70.8, 34.5, 50.6
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
using namespace std;
class student{
public:
string id, name;
map<string, double> course;
student() : id(""), name("") {
course.clear();
}
void set1(string id_, string name_) {
id = id_;
name = name_;
}
void set2(string a, double b) {
course[a] = b;
}
bool operator < (const student& tmp) const {
return id < tmp.id;
}
};
int main() {
string in;
set<string> s;
vector<student> res;
map<string, int> mp;
while (getline(cin, in)) {
int n = 0;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
n++;
}
}
switch (n) {
case 1: {
int ad = -1;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
ad = i;
break;
}
}
string id, name;
id = in.substr(0, ad);
name = in.substr(ad+2);
if (!mp.count(id)) {
mp[id] = res.size();
student tmp;
tmp.set1(id, name);
res.push_back(tmp);
} else {
ad = mp[id];
res[ad].set1(id, name);
}
break;
}
case 2: {
int ad1 = -1, ad2 = -1;
for (int i = 0; i < in.size(); i++) {
if (in[i] == ',') {
if (ad1 == -1) {
ad1 = i;
} else {
ad2 = i;
break;
}
}
}
string id, course, grade;
id = in.substr(0, ad1);
course = in.substr(ad1+2, ad2-ad1-2);
grade = in.substr(ad2+2);
s.insert(course);
if (!mp.count(id)) {
mp[id] = res.size();
student tmp;
tmp.set2(course, atof(grade.c_str()));
res.push_back(tmp);
} else {
int ad = mp[id];
res[ad].set2(course, atof(grade.c_str()));
}
break;
}
}
}
cout << "student id, name";
for (auto i: s) {
cout << ", " << i;
}
cout << ", average" << endl;
vector<double> sum(s.size(), 0), cnt(s.size(), 0);
sort(res.begin(), res.end());
int tot = 0;
double ave = 0;
for (int i = 0; i < res.size(); i++) {
cout << res[i].id << ", "<< res[i].name;
int ad = 0;
tot = 0;
ave = 0;
for (auto j: s) {
cout << ", ";
if (res[i].course.count(j)) {
cout << fixed << setprecision(1) << res[i].course[j];
sum[ad] += res[i].course[j];
cnt[ad]++;
ave += res[i].course[j];
tot++;
}
ad++;
}
cout << ", " << fixed << setprecision(1) << ave/tot << endl;
}
cout << ", ";
tot = 0;
ave = 0;
for (int i = 0; i < sum.size(); i++) {
tot++;
ave += sum[i]/cnt[i];
cout << ", " << fixed << setprecision(1) << sum[i]/cnt[i];
}
cout << ", " << fixed << setprecision(1) << ave/tot << endl;
return 0;
}
13+编程题7-1 查找成绩并折算后输出
7-1 查找成绩并折算后输出
文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found.
由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。

输入格式:
Smith 67

Anderson 75

Lewis 83

Cook 58

David 96

noname (表示结束)

Bill

输出格式:
Not found.

输入样例:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname
Lewis
输出样例:
17.43
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, int> m;
string a;
int b;
while(1)
{
cin >> a;
if (a == "noname")
break;
cin >> b;
m.insert(pair<string, int>(a, b));
}
string s;
cin >> s;
map<string, int> :: iterator p = m.begin();
p = m.find(s);
if (p != m.end())
cout << (p -> second) * 0.21 << endl;
else
cout << "Not found." << endl;
return 0;
}
7-2 电话号码同步
7-2 电话号码同步
文件phonebook1.txt和phonebook2.txt中有若干联系人的姓名和电话号码。请你设计一个程序,将这两个文件中的电话号码同步。(所谓同步,就是将两个文件中的电话号码合并后剔除相同的人名和电话号码。请将同步后的电话号码按照姓名拼音顺序排序后保存到文件phonebook3.txt中。)

由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的单词为end时,表示文件结束。将同步后的电话号码按照姓名拼音顺序排序后输出。

输入格式:
张三 13012345678

李四 13112340000

王五 13212341111

马六 13312342222

陈七 13412343333

孙悟空 13512345555

end (表示文件phonebook1.txt结束)
张三 13012345678

孙悟空 13512345555

王五 13212341111

陈七 13412343333

唐三藏 13612346666

猪悟能 13712347777

沙悟净 13812348888

end (表示文件phonebook2.txt结束)

输出格式:
陈七 13412343333

李四 13112340000

马六 13312342222

沙悟净 13812348888

孙悟空 13512345555

唐三藏 13612346666

王五 13212341111

张三 13012345678

猪悟能 13712347777

输入样例:
Zhang3 13012345678
Li4 13112340000
Wang5 13212341111
Ma6 13312342222
Chen7 13412343333
SunWuKong 13512345555
end
Zhang3 13012345678
SunWuKong 13512345555
Wang5 13212341111
Chen7 13412343333
TangSanZang 13612346666
ZhuWuneng 13712347777
ShaWuJing 13812348888
end
输出样例:
Chen7 13412343333
Li4 13112340000
Ma6 13312342222
ShaWuJing 13812348888
SunWuKong 13512345555
TangSanZang 13612346666
Wang5 13212341111
Zhang3 13012345678
ZhuWuneng 13712347777
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
set<string> s;
string s1;
for(int i=0;i<2;i++)
{
getline(cin,s1);
while(s1!="end")
{
s.insert(s1);
getline(cin,s1);
}
}

set<string>::iterator iter;
for (iter = s.begin(); iter != s.end(); ++iter)
cout << *iter << endl;

return 0;
}
7-3 姓名排序
7-3 姓名排序
从指定文本文件中读入若干学生姓名并按照拼音顺序排序后输出。
由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名,当输入的单词为end时,表示文件结束。将按照姓名拼音顺序排序后输出。

输入格式:
张三
李四
王五
马六
陈七
孙悟空
end

输出格式:
陈七 李四 马六 孙悟空 王五 张三

输入样例:
白富美
孙悟空
唐三藏
猪悟能
沙悟净
end
输出样例:
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
set<string> s;
string s1;
for(int i=0;i<2;i++)
{
getline(cin,s1);
while(s1!="end")
{
s.insert(s1);
getline(cin,s1);
}
}

set<string>::iterator iter;
for (iter = s.begin(); iter != s.end(); ++iter)
cout << *iter<<" ";

return 0;
}
STL测试
#include <iostream>
#include <vector>
#include <set>
#include <map>
using namespace std;


void testVector(){
vector<int> v;
for(int i=0;i<5;i++){
v.push_back(i);
}
for(int i=0;i<5;i++){
cout<<v[i]<<" ";//i
}
cout<<"\n";
}

void testSet(){
set<int> s;
for(int i=0;i<5;i++){
s.insert(i);
}
set<int>:: iterator item;
item=s.begin();
// while(item!=s.end()){
// cout<<*item<<" ";//i
// item++;
// }
for(item=s.begin();item!=s.end();item++) cout<<(*item)<<" ";//i
cout<<"\n";
}

void testMap(){
map<int,int> m;
for(int i=0;i<5;i++){
m.insert(pair<int,int>(i,i));
}
for(int i=0;i<5;i++){
// cout<<m[i]<<" ";//i
cout<<m.at(i)<<" ";//i
}
cout<<"\n";
}

int main(){
void (*test)(); //一个函数指针
void(*funs[])() = //一个函数指针数组
{
testVector,
testSet,
testMap
};

for(int i=0;i<3;i++){
test=funs[i];
test();
}

}


上一篇: Redis 为什么用跳表,而不用平衡树? 下一篇: 手机怎么远程登录云服务器?