cs11_adventure c++_lab1
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的更多相关文章
- cs11_c++_lab1
lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...
- goto语句引起的crosses initialization of XXX
1. 背景 goto语句虽然目前已经不提倡使用,但是用起来还是很方便,尤其是老代码中见的比较多. 在改动有goto语句的老代码时需要特别注意,是否跳过来资源的释放.有用变量的初始化等等. 很久之前写c ...
随机推荐
- 数据注解和验证 – ASP.NET MVC 4 系列
不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...
- 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
View层: $('#btnNews').click(function() { if (!confirm('确定要提交吗?')) { return; } var frontViewData = []; ...
- Request.UrlReferrer
1:Request.UrlReferrer可以获取客户端上次请求的url,这样就可以实现类似“上一页”的功能等 2:刷新当前页面,不会改变Request.UrlReferrer的值 3:如果有A,B两 ...
- Python入门2
字符串操作 字符串是语言中使用最多的,下面我们来看看python为字符串提供哪些方法: 1.upper().lower().title() 这3个方法都是返回一个新的字符串.重要性:** name = ...
- SparkConf加载与SparkContext创建(源码阅读一)
即日起开始spark源码阅读之旅,这个过程是相当痛苦的,也许有大量的看不懂,但是每天一个方法,一点点看,相信总归会有极大地提高的.那么下面开始: 创建sparkConf对象,那么究竟它干了什么了类,从 ...
- 设计模式-GoF
资源: 下载GOF的书籍: http://download.csdn.net/download/quanbove/6534569 这里总结了模式,但讲的并不准确: http://www.runoob. ...
- php判断是否是微信客户端的浏览器访问
代码: $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'MicroMessenger') === false) ...
- 使用Junit对Spring进行单元测试实战小结
Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...
- 利用nodejs搭建服务器,测试AJAX
最近学习了AJAX一直没有进行过测试,前今天了解了Noejs搭建本地服务器下就尝试了一下.通过AJAX请求的方式获取HTTP服务器返回数据的代码 首先创建一个serve.js的文件.并写入以下代码. ...
- SSH配置免密码登陆
1.使用SSH-keygen,然后一路回车使之生成id_rsa何id_rsa.pub文件,id_rsa.pub为公匙文件. 2.使用命令:cat ~/.ssh/id_rsa.pub >> ...