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; }
cltudecomp(int lb, int ub):indx(lb, ub), indx2(lb, ub) { ivector iv(lb + 1, ub); iv.fill_seqadd(lb, 1); L.allocate(lb + 1, ub, lb, iv); ivector iv1(lb, ub); iv1.fill_seqadd(lb, 1); U.allocate(lb, ub, lb, iv1); indx2.fill_seqadd(lb, 1); }
cltudecomp_for_adjoint(int lb, int ub, int n, int m):indx(lb, ub), dfclu(lb, ub), pMpos(0) { ivector iv(lb + 1, ub); iv.fill_seqadd(lb, 1); L.allocate(lb + 1, ub, lb, iv, 1, n); ivector iv1(lb, ub); iv1.fill_seqadd(lb, 1); U.allocate(lb, ub, lb, iv1, 1, m); }
void allocate(int lb, int ub) { indx.allocate(lb, ub); indx2.allocate(lb, ub); ivector iv(lb + 1, ub); iv.fill_seqadd(lb, 1); L.allocate(lb + 1, ub, lb, iv); ivector iv1(lb, ub); iv1.fill_seqadd(lb, 1); U.allocate(lb, ub, lb, iv1); indx2.fill_seqadd(lb, 1); }
void allocate(int lb, int ub, int n, int m) { indx.allocate(lb, ub); indx2.allocate(lb, ub); dfclu.allocate(lb, ub); ivector iv(lb + 1, ub); iv.fill_seqadd(lb, 1); L.allocate(lb + 1, ub, lb, iv, 1, n); ivector iv1(lb, ub); iv1.fill_seqadd(lb, 1); U.allocate(lb, ub, lb, iv1, 1, m); }
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; }
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; }