int main() { LISTINT test; for (int i = 0; i < 5; i++) { test.push_back(i+1); } //show LISTINT::iterator it = test.begin(); for (; it != test.end(); it++) { cout << *it << "\t";//12345 } cout << endl; //reversr_show LISTINT::reverse_iterator rit = test.rbegin(); // 专门还有这个迭代器[逆向迭代器] for (; rit != test.rend(); rit++) { cout << *rit << "\t";//54321 } cout << endl; getchar(); return 0; }
int main(void) { LISTINT listOne;//创建一个名为listOne的list对象 LISTINT::iterator i;//声明i为迭代器 listOne.push_front (2);//从list容器前面添加数据 listOne.push_front (1); listOne.push_back (3);//从list容器后面添加数据 listOne.push_back (4); cout<<"listOne.begin()--- listOne.end():"<<endl;//从后面显示数据 for (i = listOne.begin(); i != listOne.end(); ++i) cout << *i << ""<<endl; //从后向后显示listOne中的数据 LISTINT::reverse_iterator ir; cout<<"listOne.rbegin()---listOne.rend():"<<endl; for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) { cout << *ir << ""; } cout << endl; //使用STL的accumulate(累加)算法 int result = accumulate(listOne.begin(), listOne.end(),0); cout<<"Sum="<<result<<endl; cout<<"------------------"<<endl; //-------------------------- //用list容器处理字符型数据 //-------------------------- //用LISTCHAR创建一个名为listOne的list对象 LISTCHAR listTwo; //声明i为迭代器 LISTCHAR::iterator j; //从前面向listTwo容器中添加数据 listTwo.push_front ('A'); listTwo.push_front ('B'); //从后面向listTwo容器中添加数据 listTwo.push_back ('x'); listTwo.push_back ('y'); //从前向后显示listTwo中的数据 cout<<"listTwo.begin()---listTwo.end():"<<endl; for (j = listTwo.begin(); j != listTwo.end(); ++j) cout << char(*j) <<" "; cout << endl; //使用STL的max_element算法求listTwo中的最大元素并显示 j=max_element(listTwo.begin(),listTwo.end()); cout << "The maximum element in listTwo is:"<<char(*j)<<endl; return 0; }
void algo(){ LISTINT listOne; for(int i = 0 ; i < game_num ; ++i ){ game_fig[i] = arith(); } befor = remain ; remain = 0; rem = 0; if(game_num % 2 == 0) { for(int i = 0 ; i + 1 <= game_num ; i = i+2 ) { if( game_fig[i] == rock ){ remain++; rem++; if(game_fig[i+ 1] == rock) { // out << i << " and " << i+1 <<"あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; } else if(game_fig[i+ 1] == scis) { // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == fabr) { // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } } else if(game_fig [i] == scis) { rem++; remain++; if(game_fig[i + 1] == scis) { // out << i << " and " << i+1 << "あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; }else if(game_fig[i+ 1] == fabr) { // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == rock) { // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } } else if(game_fig[i] == fabr) { rem++; remain++; if(game_fig[i +1] == fabr) { // out << i << " and "<< i+1 << "あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; }else if(game_fig[i+ 1] ==rock) { // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == scis) { // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } } } } else { for(int i = 0 ; i + 2 <= game_num ; i = i+2 ) { if( game_fig[i] == rock ) { rem++; remain++; if(game_fig[i+ 1] == rock) { //out << i << " and " << i+1 <<"あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; }else if(game_fig[i+ 1] == scis) { // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == fabr) { // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } }else if(game_fig [i] == scis){ rem++; remain++; if(game_fig[i + 1] == scis){ // out << i << " and " << i+1 << "あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; }else if(game_fig[i+ 1] == fabr){ // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == rock){ // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } }else if(game_fig [i] == fabr){ rem++; remain++; if(game_fig[i +1] == fabr){ // out << i << " and "<< i+1 << "あいこ" <<endl; listOne.push_back(i); listOne.push_back(i+1); remain++ ; }else if(game_fig[i+ 1] ==rock){ // out << i << "の勝ち" << endl; listOne.push_back(i); }else if(game_fig[i+ 1] == scis){ // out << i+1 << "の勝ち" << endl; listOne.push_back(i+1); } } } remain ++; listOne.push_back(game_num); } LISTINT::iterator ix; num = 1; /* out << "remain element: \n" << endl; for (ix = listOne.begin(); ix != listOne.end(); ++ix){ out << *ix << "\t"; if( num % 5 == 0 ) { out<< "\n"<< endl; } num++; }*/ /* out << "\n" << endl; out <<"remain:" << remain<< endl; out <<"rem:" <<rem <<"\n" <<endl; */ listOne.erase(listOne.begin(),listOne.end()); count_rem += rem; }