Ejemplo n.º 1
1
void basic_algo(){
    cout<<endl<<"basic_algo :"<<endl;
    int ia[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8 };
    vector<int> iv1(ia,ia+5);	vector<int> iv2(ia,ia+9);
    cout<<iv1<<endl<<iv2<<endl;
    pair<vector<int>::iterator,vector<int>::iterator> p = mismatch(iv1.begin(),iv1.end(),iv2.begin());	//返回在游标的哪个位置不匹配
    if(p.first != iv1.end())	cout<<*(p.first)<<endl;
    if(p.second != iv2.end())	cout<<*(p.second)<<endl;

    cout<<equal(iv1.begin(),iv1.end(),iv2.begin())<<endl;	//比较容器内内容
    cout<<equal(iv1.begin(),iv1.end(),&ia[3])<<endl;
    cout<<equal(iv1.begin(),iv1.end(),&ia[3],less<int>())<<endl;

    fill(iv1.begin(),iv1.end(),9);			//区间填充
    cout<<iv1<<endl;
    fill_n(iv1.begin(), 3, 7);		//区间n填充
    cout<<iv1<<endl;
    vector<int>::iterator iter1,iter2;
    iter1 = iter2 = iv1.begin();
    advance(iter2,3);						//游标向前移动
    iter_swap(iter1,iter2);					//交换两个游标内容
    cout<<iv1<<endl;
    swap(*iv1.begin(),*iv2.begin());	//交换两个游标内容
    cout<<iv1<<endl<<iv2<<endl;
    string stra1[] = {"jk","jK1","jk2"};	string stra2[] = {"jk","jk1","jk3"};
    cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2)<<endl;
    cout<<lexicographical_compare(stra1,stra1+2,stra2,stra2+2,greater<string>())<<endl;

    copy(iv2.begin()+1,iv2.end(),iv2.begin());		//全部元素向前移一格
    cout<<iv2<<endl;
    copy_backward(iv2.begin(),iv2.end()-1,iv2.end());	//全部元素向后移一格
    cout<<iv2<<endl;
}
Ejemplo n.º 2
0
void pure_numeric_algo(){
    cout<<endl<<"pure_numeric_algo :"<<endl;
    int ia[11] = {0, 1, 2, 3, 4, 5, 6,6,6, 7, 8 };
    vector<int> iv(ia,ia+11);	vector<int> iv2(ia+6,ia+8);	vector<int>::iterator itr;
    itr = adjacent_find(iv.begin(),iv.end(), equal_to<int>());	//找到相邻元素相等的第一个元素
    cout<<"adjacent_find: "<<*itr<<endl;
    cout<<"count: "<<count(iv.begin(),iv.end(), 6)<<endl;	//找到元素值等于6的个数
    cout<<"count_if: "<<count_if(iv.begin(),iv.end(), bind2nd(less<int>() , 7))<<endl;		//找到小于7的元素个数
    itr = find(iv.begin(),iv.end(), 4);				//找到元素等于4的第一个元素位置
    cout<<"find: "<<*itr<<endl;
    itr = find_if(iv.begin(),iv.end(), bind2nd(greater<int>() , 2));				//找到元素大于2的第一个元素位置
    cout<<"find_if: "<<*itr<<endl;
    itr = find_end(iv.begin(),iv.end(), iv2.begin(),iv2.end());				//找到iv序列中最后子序列匹配出现的位置
    cout<<"find_end: "<<*(itr+3)<<endl;
    itr = find_first_of(iv.begin(),iv.end(), iv2.begin(),iv2.end());			//找到iv序列中最先子序列匹配出现的位置
    cout<<"find_end: "<<*(itr+3)<<endl;
    remove(iv.begin(),iv.end(), 6);				//删除元素,向前移,但是容器size不变,后面会剩余数据
    cout<<"remove: "<<iv<<endl;
    vector<int> iv3(12,-1);
    remove_copy(iv.begin(),iv.end(), iv3.begin(), 6);	//删除元素,将数据拷贝到新容器,后面会剩余数据
    cout<<"remove_copy: "<<iv3<<endl;
    remove_if(iv.begin(),iv.end(), bind2nd(less<int>(), 6));	//删除小于6的元素,后面会剩余数据
    cout<<"remove_if: "<<iv<<endl;
    remove_copy_if(iv.begin(),iv.end(), iv3.begin(), bind2nd(less<int>(), 7));		//删除小于7的元素,并拷贝到新容器
    cout<<"remove_copy_if: "<<iv3<<endl;
    replace(iv.begin(),iv.end(), 6, 3);			//将所有元素值为6的改为3
    cout<<"replace: "<<iv<<endl;
    replace_copy(iv.begin(),iv.end(),iv3.begin(), 3, 5);			//将所有元素值为3的改为5,结果保存在新容器中
    cout<<"replace_copy: "<<iv3<<endl;
    replace_if(iv.begin(),iv.end(), bind2nd(less<int>(),5), 2);			//将所有元素值小于5的改为2
    cout<<"replace_if: "<<iv<<endl;
    replace_copy_if(iv.begin(),iv.end(),iv3.begin(), bind2nd(equal_to<int>(),8), 9);			//将所有元素值为8的改为9,结果保存在新容器中
    cout<<"replace_copy_if: "<<iv3<<endl;
    reverse(iv.begin(),iv.end());			cout<<"reverse: "<<iv<<endl;		//反转
    reverse_copy(iv.begin(),iv.end(),iv3.begin());		cout<<"reverse_copy: "<<iv3<<endl;	//反转,结果保存在新容器
    rotate(iv.begin(),iv.begin() + 4, iv.end());	cout<<"rotate: "<<iv<<endl;			//互换元素
    rotate_copy(iv.begin(),iv.begin() + 5,iv.end(),iv3.begin());		cout<<"rotate_copy: "<<iv3<<endl;	//互换元素,结果保存在新容器
    int ia2[] = {2, 8};		vector<int> iv4(ia2,ia2+2);
    cout<<"search:  "<<*search(iv.begin(),iv.end(),iv4.begin(),iv4.end())<<endl;		//查找子序列出现的第一次出现地点
    swap_ranges(iv4.begin(),iv4.end(),iv.begin());				//按区域交换
    cout<<"swap_ranges:  "<<iv<<endl<<iv4<<endl;
    transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(), 2));		//所有元素减2
    cout<<"transform:  "<<iv<<endl;
    transform(iv4.begin(),iv4.end(),iv.begin(),iv4.begin(),plus<int>());		//区间对应元素相加
    cout<<"transform:  "<<iv4<<endl;
    /************************************************************************/
    vector<int> iv5(ia,ia+11);	vector<int> iv6(ia+4,ia+8);	vector<int> iv7(15);
    cout<<"max_element:  "<<*max_element(iv5.begin(), iv5.end())<<endl;		//最大元素游标
    cout<<"min_element:  "<<*min_element(iv5.begin(), iv5.end())<<endl;
    cout<<"includes:  "<<includes(iv5.begin(),iv5.end(),iv6.begin(),iv6.end())<<endl;	//iv6中元素是不是都在iv5中,这两个必须排过序
    merge(iv5.begin(),iv5.end(),iv6.begin(),iv6.end(),iv7.begin());	//两个排序号的容器合并
    cout<<"merge:  "<<iv7<<endl;
    partition(iv7.begin(),iv7.end(),bind2nd(equal_to<int>(), 5));	//满足条件的放在左边,不满足条件的放在右边
    cout<<"partition:  "<<iv7<<endl;
    unique(iv5.begin(),iv5.end());				//去重,重复的元素放在后面
    cout<<"unique:  "<<iv5<<endl;
    unique_copy(iv5.begin(),iv5.end(),iv7.begin());				//去重,结果保存在新容器
    cout<<"unique_copy:  "<<iv7<<endl;
}
Ejemplo n.º 3
0
void sort_algo_demo(){
    cout<<endl<<"sort_algo_demo :"<<endl;
    int ia[] = {0, 7,  3, 11, 5, 9, 4 , 2, 8};
    vector<int> iv(ia,ia+9);vector<int> iv1(9,0);
    vector<int>::iterator itr;
    cout<<iv<<endl;
//	partial_sort_copy()
    partial_sort(iv.begin(),iv.begin()+5,iv.end());		//内部采用堆算法,保证前面的middle-first有序
    partial_sort_copy(iv.begin(),iv.end(),iv1.begin(),iv1.begin()+4,greater<int>());
    cout<<"partial_sort: "<<iv<<endl<<"partial_sort_copy: "<<iv1<<endl;
    sort(iv.begin(),iv.end(),greater<int>());
    cout<<"sort: "<<iv<<endl;

    int ia1[] = {1, 3,  5, 7, 2, 4, 6 , 8, 10};
    vector<int> iv2(ia1,ia1+9);	vector<int> iv3(ia1,ia1+9);
    inplace_merge(iv2.begin(),iv2.begin()+4,iv2.end());
    cout<<"inplace_merge: "<<iv2<<endl;
    nth_element(iv3.begin(),iv3.begin()+5,iv3.end());
    cout<<"nth_element: "<<iv3<<endl<<"5th element: "<<*(iv3.begin()+5)<<endl;

}
Ejemplo n.º 4
0
int main()
{
	std::cout<<"test move constructor:\n";
	std::allocator<std::string> alloc;
	size_t size = 5;
	auto old_strs = alloc.allocate(size);
	for(size_t i = 0; i < size; i++)
	{
		alloc.construct(old_strs + i, "abcde");
	}
	std::cout<<"old_strs[0]: "<<old_strs[0]<<std::endl;
	auto new_strs = alloc.allocate(size);
	for(size_t i = 0; i < size; i++)
	{
		alloc.construct(new_strs + i, std::move(*(old_strs + i)));
	}
	std::cout<<"new_strs[0]: "<<new_strs[0]<<std::endl;
	std::cout<<"old_strs[0]: "<<old_strs[0]<<std::endl;
	for(size_t i = 0; i < size; i++)
	{
		alloc.destroy(old_strs + i);
	}
	alloc.deallocate(old_strs, size);
	std::cout<<"test move constructor done.\n"<<std::endl;

	std::cout<<"test rvalue reference:\n";
	int j = 42;
	int &lr = j;
	//int &&rr = j; // Wrong. Can't bind a rvalue ref to a lvalue.
	//int &lr2 = i * 42; // Wrong. Can't bind a lvalue ref to a rvalue.
	const int &lr3 = j * 42;
	int &&rr2 = j * 42;
	//int &&rr3 = rr2; // Wrong. rr2 is a rvalue ref and rvalue ref is a lvalue.
	int &lr4 = rr2;
	std::cout<<j<<'\t'<<lr<<'\t'<<lr3<<'\t'<<rr2<<'\t'<<lr4<<std::endl;
	std::cout<<"test rvalue ref done.\n"<<std::endl;

	std::cout<<"test std::move:\n";
	std::string str5 = "asdf";
	std::string &lr5 = str5;
	std::string &&rr5 = std::move(str5);
	rr5[0] = 'b';
	lr5[1] = 'z';
	std::cout<<rr5<<'\t'<<lr5<<'\t'<<str5<<std::endl;
	std::cout<<"test std::move done.\n"<<std::endl;
	
	std::cout<<"test custom move copy constructor/move assign operator.\n";
	IntVec iv1(10);
	for(size_t i = 0; i < 5; i++)
		iv1.push_back(i);
	std::cout<<"-------iv1:\n";
	iv1.print_info();

	IntVec iv2(std::move(iv1));
	std::cout<<"-------iv2:\n";
	iv2.print_info();
	std::cout<<"-------iv1:\n";
	iv1.print_info();

	IntVec iv3 = iv2;
	std::cout<<"-------iv3:\n";
	iv3.print_info();
	std::cout<<"-------iv2:\n";
	iv2.print_info();

	IntVec iv4(5);
	std::cout<<"-------iv4:\n";
	iv4.print_info();
	iv4 = std::move(iv2);
	std::cout<<"-------iv4:\n";
	iv4.print_info();
	std::cout<<"-------iv2:\n";
	iv2.print_info();
	std::cout<<"test custom move copy constructor/move assign operator done.\n"<<std::endl;

	std::cout<<"test move iterator:\n";
	auto new_strs2 = alloc.allocate(size);
	std::uninitialized_copy(std::make_move_iterator(new_strs),
			std::make_move_iterator(new_strs + size),
			new_strs2);
	std::cout<<"new_strs[0]: "<<new_strs[0]<<std::endl;
	std::cout<<"new_strs2[0]: "<<new_strs2[0]<<std::endl;
	for(size_t i = 0; i < size; i++)
	{
		alloc.destroy(new_strs + i);
	}
	alloc.deallocate(new_strs, size);
	std::cout<<"test move iterator done.\n"<<std::endl;

	std::cout<<"test ref folding:\n";
	int val = 2;
	int &lref = val;
	int &&rref = 2;
	std::cout<<"-------with val:\n";
	vague_func(2);
	std::cout<<"-------with lref:\n";
	vague_func(lref);
	std::cout<<"-------with rref:\n";
	vague_func(rref);
	vague_func(std::move(val));
	std::cout<<"test ref done.\n"<<std::endl;

	std::cout<<"test forward:\n";
	forward_func(f, 5);
	forward_func(g, rref);
	forward_func(g, val);
	std::cout<<"test forward done.\n"<<std::endl;
}
Ejemplo n.º 5
0
 iv2 get_window_size()
 {
   return iv2(window_width, window_height);
 }