随笔分类 - C++训练
摘要:10.29 编写程序,使用流迭代器读取一个文本文件,存入一个vector中的string里。#include#include#include#include#includeusing namespace std;int main(){ ifstream in("1.txt"); istr...
阅读全文
摘要:10.24 给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值。#include#include#include#include#includeusing namespace std;bool check_size(vector::...
阅读全文
摘要:10.21 编写一个lambda,捕获一个局部int变量,并递减变量值,直至它变为0.一旦变量变为0,再调用lambda应该不再递减变量。lambda应该返回一个bool值,指出捕获的变量是否为0.#include#includeusing namespace std;int main(){ ...
阅读全文
摘要:编写程序,求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。使用find_if实现的代码如下:#include#include#include#includeusing namespace std;void biggies(vector &words,vect...
阅读全文
摘要:10.13 标准库定义了名为partition的算法,它接受一个谓词,对容器内容进行划分,使得谓词为true的值会排在容器的前半部分,而使谓词为false的值会排在后半部分。算法返回一个迭代器,指向最后一个使用谓词为true的元素之后的位置。编写程序,接受一个string,返回一个bool值,指出s...
阅读全文
摘要:10.11编写程序,使用stable_sort和isShorter将传递给你的elimDups版本的vector排序。打印vector的内容。#include#include#include#includeusing namespace std;void elimDup(vector &words)...
阅读全文
摘要:9.52使用stack对象处理带圆括号的表达式。遇到左圆括号时,将其标记下来。当你在一个左括号之后遇到右圆括号时,弹出stack对象中这两边括号之间的元素,直到遇到左括号,将左括号也一起弹出栈。接着在stack对象中压入一个值,用以表明这个用一对圆括号括起来的表达式已经被替换。程序如下:#inclu...
阅读全文
摘要:栈使用在括号匹配中的例子程序如下:#include#includeusing namespace std;int main(){ stack initStack; char ch; while(cin>>ch) { if(ch=='('||ch=='{'||ch...
阅读全文
摘要:9.51 设计一类,它又三个unsigned成员,分别表示年月日。为其编写构造函数,接受一个表示日期的string参数。程序如下:#include#includeusing namespace std;class My_Date{public: My_Date(const string &s)...
阅读全文
摘要:9.50 编写程序处理一个vector,其元素都表示整数型。计算vector中所有元素之和。修改程序,使之计算表示浮点值的string之和。程序如下:#include#include#includeusing namespace std;int main(){ vector str={"1",...
阅读全文
摘要:9.47 编写程序,首先查找string"ab2c3d7R4E6"中的每个数字字符,然后查找其中每个字母字符。编写两个版本的程序,第一个要使用find_first_of,第二个要使用find_first_not_of。程序如下:#include#includeusing namespace std;...
阅读全文
摘要:9.43 编写一个函数,接受三个string参数s、oldVal和newVal。使用迭代器及insert和erase函数将s中所有oldval替换为newVal。测试程序,用它替换通用的简写形式,如,将“tho”替换为“though”,将“thru”替换为“through”。程序如下:#includ...
阅读全文
摘要:9.28 编写函数,接受一个forward_list和两个string共三个参数。函数应在链表中查找第一个string,并将第二个string插入到紧接着第一个string之后的位置。若第一个string未在链表中,则将第二个string插入到链表末尾。#include#include#includ...
阅读全文
摘要:#include#includeusing namespace std;int main(){ forward_list flst={0,1,2,3,4,5,6,7,8,9}; auto prev=flst.before_begin(); auto curr=flst.begin(...
阅读全文
摘要:#include#include#includeusing namespace std;int main(){ vector vec={0,1,1,2,3,5,8,21,55,89}; list li={0,1,1,2,3,5,8,21,55,89}; auto ve=vec.be...
阅读全文
摘要:假如有一个文件,列出了一些人和他们的电话号码。某些人只有一个号码,而另外一些人则有多个——家庭电话、工作电话、移动电话等。我们的输入文件看起来是这样的:morgan 2015552368 8625550123drew 9735550130lee 6095550132 2015550175 80055...
阅读全文
摘要:编写程序,将来自一个文件中的行保存在一个vector中,然后使用一个istringstream从vector读取数据成员,每次读取一个单词#include #include #include#include#includeusing namespace std;int main(){ ifst...
阅读全文
摘要:#include#include#include#includeusing namespace std;int main(int argc,char *argv[]){ ifstream input(argv[1]); vector vec; string tmp; whil...
阅读全文
摘要:编写函数的声明,令其接受两个int形参并且返回类型也是int;然后声明一个vector对象,令其元素是指向该函数的指针。编写4个函数,分别对两个int值执行加、减、乘除运算。#include#include#includeusing namespace std;int plus1(int x,int...
阅读全文
摘要:#include#include#includeusing namespace std;int main(){ vector vec1={0,0,1,1,2,3,5,8}; vector vec2={5,8}; decltype(vec1.size()) j=0,m=0; f...
阅读全文

浙公网安备 33010602011771号