exercise1.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm> using namespace std; int myFunction()
{
return rand() % + ;
} int main()
{
vector<int> vi1();
generate(vi1.begin(), vi1.end(), myFunction);//可用generate函数初始化,与下功能一致
/*
for(int i = 0; i < 100; i++)
{
vi1[i] = (rand() % 100 + 1);
}
*/
vector<int> vi2(vi1.size());
copy(vi1.begin(), vi1.end(), vi2.begin()); for(int i = ; i < ; i++)
cout << i << " "<< vi1[i] << " " << vi2[i] << endl;
}

exercise2.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <string> using namespace std; string randomString()
{
int i = rand() % + ;//随机字符串长度
string str = "";
for(int j = ; j < i; j++)
{
str += 'a' + rand() % ;//随机字符串中内容
}
str += '\0';//字符串末尾处理
return str;
} bool myFunction(string s1, string s2)//自定义谓词1
{
return s1.size() < s2.size();
} struct myClass//自定义谓词2
{
bool operator()(string s1, string s2)
{
return s1.size() < s2.size();
}
}myObject; /*
class myClass//自定义谓词2
{
public:
bool operator()(string s1, string s2)
{
return s1.size() < s2.size();
}
};
myClass myObject;
*/ int main()
{
vector<string> vs();
vector<string>::iterator vsi; for(vsi = vs.begin(); vsi != vs.end(); vsi++)//填充随机字符串
{
*vsi = randomString();
} for(int j = ; j < ; j++)//输出原始随机字符串
{
cout << j << " " << vs[j] << endl;
}
cout << "原始值----------------------------" << endl; sort(vs.begin(), vs.end());//默认排序并输出
for(int j = ; j < ; j++)
{
cout << j << " " << vs[j] << endl;
}
cout << "第一次字母序排序------------------------"<< endl; sort(vs.begin(), vs.end(), myFunction);//自定义谓词1排序并输出
sort(vs.begin(), vs.end(), myObject);//自定义谓词2排序并输出
for(int j = ; j < ; j++)
{
cout << j << " " << vs[j] << endl;
}
cout << "第二次按长度排序------------------------"<< endl;
}

exercise3.cc

 #include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <string> using namespace std; int randomNum()
{
return rand() % + ;
} struct myClass//自定义谓词
{
int even;
int odd;
bool operator()(int num)
{
if(num % )
{
even++ ;
return true;
}
else
{
odd++;
return false;
}
}
}myObject; int main()
{
vector<int> vi();
vector<int>::iterator itor; for(itor = vi.begin(); itor != vi.end(); itor++)//填充随机字符串
{
*itor = randomNum();
} for(int i = ; i < ; i++)//输出原始随机字符串
{
cout << i << " " << vi[i] << endl;
}
cout << "原始值----------------------------" << endl; myClass result = for_each(vi.begin(), vi.end(), myClass());//第一种调用方式,带状态
myClass result = for_each(vi.begin(), vi.end(), myObject);//第二种调用方式,但是,其中odd和even是如何初始化为零的?
cout << result.odd << endl << result.even << endl;
}

exercise4.cc

 #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <ext/functional>//为支持compose1函数额外添加的头文件 using namespace std; int main()
{
//初始化vector
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(); //把vector中内容通过指定的流迭代器写入到指定流中,放一个整数,后跟一个“ ”
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl; //remove_if移除序列中谓词返回true的元素,但是容器长度不变,所有元素还在容器中,其实是把所有应移除元素至于容器尾部并返回一个分界迭代器
//compose1(f,g)函数执行顺序是f(g),先执行g,把g的结果作为f的参数
//bind2nd函数是把一个二元谓词转化成一元谓词的函数,绑定第二个参数,使之成为一个一元谓词
//modulus函数是取模函数,被绑定模2
//那么所有的偶数都被标记,非偶数都被至于前面,返回的是指向8的迭代器
vector<int>::iterator new_end =
remove_if(v.begin(), v.end(),
__gnu_cxx::compose1(bind2nd(equal_to<int>(), ),
bind2nd(modulus<int>(), ))); copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}

exeercise5.cc

 #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator> using namespace std; template <typename BidirectionalIterator>
void my_reverse(BidirectionalIterator first, BidirectionalIterator last)
{
if(distance(first, last) == )//当什么都没有的情况
{
cout<<"为零换个屁啊"<<endl;
return;
}
else
{
while(distance(first, last-) > )//这是考虑除了迭代器重合外的所有情况,只有在两迭代器指向不同位置的时候才会对换,指向同一位值就不对换了
{
cout<<*first<<"---"<<*(last-)<<endl;//对换的结构
swap(*first, *(last-));//注意,不是first和last对换,那是迭代器互相赋值,不对。也不是*first和*last之间的对换,因为last指向的是最后元素之后的元素,也不对。
first++;
last--;
}
}
} int myFunction()
{
return rand() % + ;
} int main()
{
int size;
cout << "input size: " << endl;
cin >> size;
vector<int> vi(size); generate(vi.begin(), vi.end(), myFunction);
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
cout << endl; my_reverse(vi.begin(), vi.end());
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}

cs11_adventure c++_lab1的更多相关文章

  1. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

  2. goto语句引起的crosses initialization of XXX

    1. 背景 goto语句虽然目前已经不提倡使用,但是用起来还是很方便,尤其是老代码中见的比较多. 在改动有goto语句的老代码时需要特别注意,是否跳过来资源的释放.有用变量的初始化等等. 很久之前写c ...

随机推荐

  1. 2016HUAS_ACM暑假集训3F - Jungle Roads

    这个题目属于最小生成树问题,可以用Prim,也可以用Kruskal(还没试).题意简单直接,给你一个图,求出它最小生成树的权值. 题目最有趣的地方就是图的顶点是字母,稍微处理一下就好了. Sample ...

  2. 有关默认相机转VR相机

    呃...15年开篇~ 去年想写一个有关默认相机转VR相机的脚本,当时没写完,今天不小心翻到并写完了,而且思路也和原来完全不一样了,增加了是否删除原相机与是否转换所选相机的选项. 由于国内VR版本比较混 ...

  3. JS时间格式 GMT格式转换

    JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...

  4. 空的安卓工程添加activity

    1.编写类继承activity,并重写onCreate方法 package org.tonny; import android.app.Activity; import android.os.Bund ...

  5. DTO对象

    在EF中,EF生成的对象都是代理对象,这些对象看上去是实体类对象,但是其实都是EF封装好的代理类对象.所以调用EF查询得到的代理类对象有继承于实体对象,所以可以用实体类对象来接收返回的代理类对象.EF ...

  6. ibm v3700

    raid5总容量计算(n-1)*最小盘容量RAID0:N块盘组成,逻辑容量为N块盘容量之和:RAID1:两块盘组成,逻辑容量为一块盘容量:RAID3:N+1块盘组成,逻辑容量为N块盘容量之和:RAID ...

  7. Perforce P4V,添加映射

  8. C++输入输出流格式控制

    来源:http://blog.csdn.net/virtualdesk/article/details/5355793 1.使用控制符控制输出格式 控制符 作用 dec 设置整数的基数为10 hex ...

  9. 触发器事件trigger

    修改mysql结束符   delimiter name 触发器语法:     create trigger 触发器名称       after/before  触发时间     //错误  ERROR ...

  10. Python 2.7.x 和 3.x 版本的重要区别

    许多Python初学者都会问:我应该学习哪个版本的Python.对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本.等学得差不多了,再 ...