Standard Template Library

Source: Internet
Author: User

C + + study notes VI

The Standard Template Library is part of the C + + standard library and is a frequently used template in C + + programming. The common structure of this is described below. Mainly take string,vector,map,list as an example.

1.vector

General usage: #include <vector>

using namespace Std;

Vector<int > arr;

Common functions:

Push_back ()//Tail add

Pop_back ();//Tail Delete

Clear ();//Delete all elements

Vector: Dynamically increases memory,

arr.at (1) =1;<=>arr[1]=1;

Using iterators:

Vector<int>:;iterator ITER;

For (Iter=arr.begin (); Iter!=arr.end (); iter++) {

int &value =*iter;

printf ("%d\t", Arr[i]);

}

Vector is not suitable for inserting delete operations

2.list templates (linked list)

Push_back,pop_back

Push_front,pop_front

#include <list>

using namespace Std;

List does not support random access

List is more suitable for insert operations than vectors.

Importance: Vector is dispensable and list is indispensable


Class object{


};

List<object> Ls;//ls Object

Ls.push_back (Object (1, "Zhang San"));

Ls.push_back (Object (2, "John Doe"));

To define an iterator:

List<object>::iterator iter;//iterator is a pointer type

For (Iter=ls.begin (); Iter!=ls.end (); iter++) {

Object &value=*iter;

printf ("%d,%s", value.id,value.name);

}

Delete

For (Iter=ls.begin (); Iter!=ls.end (); iter++) {

Object &value=*iter;

if (value.id=3) {

Ls.erase (ITER);

Break

}

}

Customizing the list template is a hassle, and it's more convenient to use the lists template.


3.string templates

Common functions: Append,clear,capacity,size,lengeh,at,find

#include <string>

using namespace Std;

String str1;//represents an empty string.

String str2 ("Abcde ', 5);

Note: cannot initialize to null

1) Adding elements

Str1.append ("Abscd ', 3);//Copy length 3;

Str1.append ("Abdcde", 1, 3);//subscript 1 to Subscript 3

Str1.append (4, ' h ');//Added 4 H characters.

Access string content: str1.at (i) =j;<==>str1[i]=j;

2) comparison

Relational operators such as ==,>= can be used directly because they are overloaded.

3) Find and extract substrings

Find (' i ');//finds the position of I and returns

RFind (' i ');//start looking for the right

T1.SUBSTR (4);//starting from the fourth character to take an element

T1.SUBSTR (4,3)//starting from fourth, length is 3;


4) string is a template type, which is passed as a function parameter, using a reference

void Test (const string &t) {}//does not change memory value

void Test (String &t) {}//Changes memory value

5) String class member function c_str ();

Returns a C-style string that is the same as the String class object itself; Converts a string object into a string format in C by using the C_STR () function in the String class.

String S1 ("abc");

printf ('%s\n ', S1.c_str ());

Example: const char *p=s1.c_str ();

printf ("%s\n", p);

6) Access to strings:

At or [] to access

String can be used directly in the standard Template Library, unlike other types.

Add: The reference must be initialized, and the pointer will be


Standard Template Library

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.