STL Learning Series III: Operation List Container

Source: Internet
Author: User

Study finished STL series BIS, Oneself wrote a program practicing! The program uses the architecture of the second series of articles. Study of one of the STL and the second, for the basic principles of STL have a fundamental understanding. In fact, about these kinds of containers, have been contacted before, but in Java, when learning is also swallowed! Now feel that is really learning taboo, or step for good. Speed can be slowed down a little, that should be solid!

Note: The program in VC6 debugging through, for not clear how to run STL under VC, you can read one of the STL series.

Tjuailab
Author:zhangbufeng
time:2005.8.23 22:00
#include <string>
#include <list>
#include <iostream>
#include <algorithm>
using namespace Std;

printstring (const string& stringtoprint);
Const string Namecode ("Bufeng");
Class Isbufeng
{
Public
BOOL Operator () (String &stringname)
{
Return Stringname.substr (0,6) ==namecode;
}
};
void Main (void)
{
Define a list
list<string>aistudentname;
list<string>targetaistudentname;
List<string>::iterator Aistudentnameiterator;
Use the member functions of the list push_back and Puch_front insert elements into the list, insert any inserts using the member function
Aistudentname.push_back ("Bufengzhang");
Aistudentname.push_back ("Yangzhang");
Aistudentname.push_back ("Kunhuang");
Aistudentname.push_front ("Chengluo");
Aistudentname.push_front ("Yonghuoyang");
Aistudentname.push_front ("Xiaoyuancui");
Aistudentname.insert (Aistudentname.end (), "Kefeigong");
Targetaistudentname.push_back ("Bufengzhang");
Targetaistudentname.push_back ("Yangzhang");
Use the list's member function empty to determine if the list is empty and size to return the number of members
if (Aistudentname.empty ())
{
cout<< "Aistudentname member is empty" <<endl;
}
Else
{
cout<< "Aistudentname Number of Members" <<aistudentname.size () <<endl;
}
Working with the For Loop and iterator on the elements of a list
The members of the cout<< "Aistudentname" are as follows (use for loop and iterator: "<<endl;
For (Aistudentnameiterator=aistudentname.begin (); Aistudentnameiterator!=aistudentname.end ();
aistudentnameiterator++)
{
cout<<*aistudentnameiterator<<endl;
}
Using STL's common algorithm for_each to process the elements in the list
cout<< "Aistudentname members are as follows (using STL's Common algorithm For_each:" <<endl;
For_each (Aistudentname.begin (), Aistudentname.end (), printstring);
Learn general algorithms for using STL count and Count_if
int numberofstudent=0;
Numberofstudent=count (Aistudentname.begin (), Aistudentname.end (), "Bufengzhang");
cout<< "Tjuailab" <<NumberofStudent<< "Bufengzhang" <<endl;
Numberofstudent=count_if (Aistudentname.begin (), Aistudentname.end (), Isbufeng ());
cout<< "Tjuailab" <<NumberofStudent<< "Bufengzhang" <<endl;
Use STL General algorithm find () Look up objects in list
Aistudentnameiterator=find (Aistudentname.begin (), Aistudentname.end (), "Bufengzhang");
if (Aistudentnameiterator==aistudentname.end ())
cout<< "Aistudentname no Bufengzhang" <<endl;
Else
cout<< "can find Bufengzhang in Aistudentname" <<endl;
Using STL General algorithm find_if to find objects in list
Aistudentnameiterator=find_if (Aistudentname.begin (), Aistudentname.end (), Isbufeng ());
if (Aistudentnameiterator==aistudentname.end ())
cout<< "Aistudentname no Bufengzhang" <<endl;
Else
cout<< "can find Bufengzhang in Aistudentname" <<endl;
Use STL General algorithm search to find a sequence in the list
Aistudentnameiterator = Search (Aistudentname.begin (), Aistudentname.end (), Targetaistudentname.begin (), Targetaistudentname.end ());
if (Aistudentnameiterator==aistudentname.end ())
cout<< "Aistudentname not found in Bufengzhang and Yangzhang" <<endl;
Else
cout<< "in Aistudentname can find Bufengzhang and Yangzhang" <<endl;
Sort a list by using the member function sort () of the list
Aistudentname.sort ();
cout<< "sorted as follows:" <<endl;
For_each (Aistudentname.begin (), Aistudentname.end (), printstring);
Delete the element by using the member function of the list (Pop_front,pop_back,erase (), remove ())
Aistudentname.pop_front ();
Aistudentname.pop_back ();
Aistudentname.erase (Aistudentname.begin ());
Aistudentname.remove ("Xiaoyuancui");
cout<< "The remaining members are as follows:" <<endl;
For_each (Aistudentname.begin (), Aistudentname.end (), printstring);
Use STL General algorithm remove () delete element from list
Aistudentnameiterator=remove (Aistudentname.begin (), Aistudentname.end (), "Yangzhang");
cout<< "Remove the remaining members after the following:" <<endl;
For_each (Aistudentname.begin (), aistudentnameiterator,printstring);

}
printstring (const string& stringtoprint)
{
cout<<stringtoprint<<endl;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.