Home>

C++ programming language has an application method called vector,Its role is very important in actual programming.Here we will introduce in detail the relevant application skills and basic content of c++ vector,I hope to bring you some help.

(1) vector<type>identifier;

(2) vector<type>identifier (maximum capacity);

(3) vector<type>identifier (maximum capacity,All initial values);

(4) int i [4]={12,3,4,5};

vector<type>vi (i, i + 2);//Get i index value after 3;

(5) vector<vector<int>>//vi defines a 2-dimensional container;Remember to have spaces,Otherwise, an error will be reported

vector<int>line
//Be sure to initialize the vi rows when using;
for (int i=0;i<10;i ++)
{
vector.push_back (line);
}
///I think it is good to use vectors to define two-dimensional arrays,Because the length can be determined in advance.
well.

(6) c++ vector sort

vector<int>vi;
vi.push_back (1);
vi.push_back (3);
vi.push_back (0);
sort (vi.begin (), vi.end ());////small to large
reverse (vi.begin (), vi.end ()) ///

(7) Sequential access

vector<int>vi;
for (int i=0;i<10;i ++)
{
vector.push_back (i);
}
for (int i=0;i<10;i ++) ///The first call method
{
cout<vector [i]<<""
}
for (vector<int>::iterator it=vi.begin ();
it!=vi.end ();it ++) ///The second method of calling
{
cout<<* it<<""
}

(8) Looking for

vector<int>vi;
for (int i=0;i<10;i ++)
{
vector.push_back (i);
}
vector<int>::interator it=find (vi.begin (), vi.end, 3);
cout<<<endl;///Returns the position where the value was found inside the container.

(9) Initialize c++ vector using array

int i [10]={1,2,3,4,5,6,7,78,8};
///The first
vector<int>vi (i + 1, i + 3);///From the second element to the third element
for (vector<int>::interator it=vi.begin ();
it!=vi.end ();it ++)
{
cout<<<<<""
}

(10) Structure type

struct temp
{
public:
string str;
public:
int id;
} tmp
int main ()
{
vector<temp>t;
temp w1;
w1.str="hellowor";
w1.id=1;
t.push_back (t1);
cout<<w1.str<<<<<<
return 0;
}
c
  • Previous Discover the new features of C # 60
  • Next C # URL short address compression algorithm and short URL principle analysis