C ++ container class object function parameter problem, container Function

Source: Internet
Author: User

C ++ container class object function parameter problem, container Function

In a word, a container-Class Object acts as a function parameter, which is the same as an integer type as a function parameter.

Validators

#include "stdafx.h"#include <iostream>#include <list>using namespace std;void do_something1(list<int> *ls){ls->push_back(1);ls->push_back(2);ls->push_back(3);return;}void do_something2(list<int> ls){ls.push_back(1);ls.push_back(2);ls.push_back(3);return;}void do_something3(list<int> &ls){ls.push_back(1);ls.push_back(2);ls.push_back(3);return;}int main(void){list<int> ls1,ls2,ls3;cout<<"**************"<<endl;cout<<"ls1:"<<endl;cout<<ls1.size()<<endl;do_something1(&ls1);cout<<ls1.size()<<endl;cout<<"**************"<<endl;cout<<"ls2:"<<endl;cout<<ls2.size()<<endl;do_something2(ls2);cout<<ls2.size()<<endl;cout<<"**************"<<endl;cout<<"ls3:"<<endl;cout<<ls3.size()<<endl;do_something3(ls3);cout<<ls3.size()<<endl;system("pause");return 0;}



Compile a template function to output all elements in the container object with the container object as the parameter

// The caller need release pOutArray

Template <class T>
Void Terminate (vector <T> & vec, T * pOutArray)
{
Int size = vec. size ();

POutArray = new T [size];

For (int I = 0; I <size; I ++)
{
* POutArray [I] = vec [I];
}
}
 
The use of the three iterator parameters passed by the member function Insert in the c ++ container

# Include <vector>
Vector belongs to the std naming field. Therefore, you need to complete the Code as follows:
Using std: vector;
Vector <int> vInts;
Or, use the full name:
Std: vector <int> vInts;
We recommend that you use the global naming domain: using namespace std;
Function
Statement
C. assign (beg, end) c. assign (n, elem)
Assign the value of the data in the [beg; end) interval to c. Assign a copy of n elem values to c.
C. at (idx)
Returns the data indicated by the index idx. If idx is out of bounds, out_of_range is thrown.
C. back ()
Returns the last data and does not check whether the data exists.
C. begin ()
Returns the first data address in the iterator.
C. capacity ()
Returns the number of data in the container.
C. clear ()
Remove all data from the container.
C. empty ()
Determines whether the container is empty.
C. end ()
Point to the last data address in the iterator.
C. erase (pos)
C. erase (beg, end)
Delete the data at the pos position and return the location of the next data.
Delete the data in the [beg, end) interval and return the location of the next data.
C. front ()
Returns the first data.
Get_allocator
Returns a copy using the constructor.
C. insert (pos, elem)
C. insert (pos, n, elem)
C. insert (pos, beg, end)
Insert an elem copy at the pos position and return the new data location. Insert n elem data at the pos position. No return value. Data inserted in the [beg, end) range at the pos position. No return value.
C. max_size ()
Returns the maximum number of data in the container.
C. pop_back ()
Delete the last data.
C. push_back (elem)
Add a data entry to the end.
C. rbegin ()
Returns the first data of a reverse queue.
C. rend ()
Returns the next location of the last data in a reverse queue.
C. resize (num)
Specify the length of the queue again.
C. reserve ()
Reserve the appropriate capacity.
C. size ()
Returns the actual number of data in the container.
C1.swap (c2)
Swap (c1, c2)
SWAps c1 and c2 elements. Same as above.
Vector <Elem>
Cvector <Elem> c1 (c2)
Vector <Elem> c (n)
Ector <Elem> c (n, elem)
Vector <Elem> c (beg, end)
C .~ Vector <Elem> ()
Create an empty vector. Copy a vector. Create a vector Containing n pieces of data, which are generated by default. Create a vector Containing n elem copies. Create a vector in the [beg; end) interval. Destroys all data and releases memory.
Operator []
Returns a reference from a specified position in the container.
Create a vector
The vector container provides multiple creation methods. The following describes several common methods.
Create an empty vector object of the Widget type:
Vector <Widget> vWidgets;
... The remaining full text>

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.