Using vector containers instead of arrays-initializing vector objects with arrays

Source: Internet
Author: User

In C + +, we cannot initialize another array directly with an array, we can only create new arrays, and then explicitly copy the elements of the original array to the new array individually.
In accordance with the practice in the C language:

Const size_t arry_size=6;

int int_arry[arry_size]={0,1,2,3,4,5};

int int_arry2[arry_size];

for (size_t ix=0;ix<arry_size;++i)
Int_arry2[ix]=int_arry[i];
Instead of initializing the vector object with an array, you can initialize another vector object directly using one of the vector objects:

#include <iostream>

#include <vector>

using namespace Std;

int main () {

Initializing a Vector object with an array
const size_t arr_size=6;
int int_arr[arr_size]={0,1,2,3,4,5};
vector<int> Ivec (int_arr,int_arr+arr_size);

/* The two pointers passed to Ivec mark the range of the vector's initial value. The second pointer points to the address space after the last element being copied. */

The range of elements being marked can be a subset of the array
vector<int> ivec1 (IVEC);

For (Vector<int>::size_type i=0;i!=ivec1.size (); ++i)
cout<<ivec1[i];
cout<<endl;
return 0;

}

After the array is converted to a vector object, you can use various function operations of the vector object, such as size () to get the number of elements, push_back () does not add new elements at the end, and so on.

Using vector containers instead of arrays-initializing vector objects with arrays

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.