A step-by-step study of STL Standard Template Library

Source: Internet
Author: User
Tags count printf

Use of List

The use of the list must include the header file #include <list>

1), how to define a list object

#include <list>
int main (void)
{
  list<char > cList; //声明了list<char>模板类 的一个实例
}

2, using the member functions of the list push_back and Push_front inserts an element into the list

cList. push_back(‘a’); //把一个对象放到一个list的后面
cList. push_front (‘b’); //把一个对象放到一个list的前面

3, using the member function of list empty () to determine whether the list is empty

if (cList.empty())
{
  printf(“this list is empty”);
}

4), using list< char >::iterator to get a pointer to the list

list< char>::iterator charIterator;
for(cIterator = cList.Begin();cIterator != cList.end();cIterator++)
{
  printf(“%c”, *cIterator);
} //输出list中的所有对象

Description: The Clist.begin () and Clist.end () function return a pointer to the list< char >::iterator, which does not support random access because the list uses a linked list structure, and therefore cannot be used with clist.begin () + 3来 points to the fourth object in the list, vector and deque support random access.

5), using the STL General Algorithm count () to count the number of elements in the list

int cNum;
char ch = ’b’;
cNum = count(cList.Begin(), cList.end(), ch); //统计list中的字符b的个数

Note: You must join #include <algorithm> before using the count () function

6), using the general algorithm of STL Count_if () to count the number of elements in the list

const char c (‘c’);
class IsC
{
public:
  bool operator() ( char& ch )
  {
    return ch== c;
  }
};
int numC;
numC = count_if (cList.begin(), cList.end(),IsC());//统计c的数量;

Description: Count_if () takes a parameter of a function object that is a class function object with at least one operator () method that returns true or FALSE when it is agreed to call operator for the STL algorithm. They judge this function according to this. For example, it would be clearer. Count_if () makes a more complex evaluation than count () by passing a function object to determine whether an object should be counted.

7), using the STL general algorithm find () in the list to look up objects

list<char >::iterator FindIterator;
FindIterator = find(cList.begin(), cList.end(), ‘c’);
If (FindIterator == cList.end())
{
  printf(“not find the char ‘c’!”);
}
else
{
  printf(“%c”, * FindIterator);
}

Note: If the specified object is not found, the value of Clist.end () is returned, and a pointer to the object iterator is returned if found.

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.