c++ stl队列初始化_声明,初始化和访问向量| C ++ STL
c++ stl隊列初始化
Here, we have to declare, initialize and access a vector in C++ STL.
在這里,我們必須聲明,初始化和訪問C ++ STL中的向量。
向量聲明 (Vector declaration)
Syntax:
句法:
vector<data_type> vector_name;Since, vector is just like dynamic array, when we insert elements in it, it automatically resize itself.
由于vector就像動態(tài)數(shù)組一樣,當我們在其中插入元素時,它會自動調整大小。
We can also use, the following syntax to declare dynamic vector i.e a vector without initialization,
我們還可以使用以下語法聲明動態(tài)向量,即不初始化的向量 ,
vector<data_type> vector_name{};If, we want to initialize a vector with initial elements, we can use following syntax,
如果要使用初始元素初始化向量,則可以使用以下語法,
vector<data_type> vetor_name{elements};矢量迭代器 (Vector iterator)
To access/iterate elements of a vector, we need an iterator for vector like containers. We can use following syntax to declare a vector iterator:
要訪問/迭代向量的元素,我們需要一個類似容器的向量迭代器。 我們可以使用以下語法來聲明向量迭代器:
vector<data_type>::iterator iterator_name;Example:
例:
vector<int>::iterator it;vector :: begin()和vector :: end()函數(shù) (vector:: begin() and vector::end() functions )
Function vector::begin() return an iterator, which points to the first element in the vector and the function vector::end() returns an iterator, which points to the last element in the vector.
函數(shù)vector :: begin()返回一個迭代器,該迭代器指向向量中的第一個元素,函數(shù)vector :: end()返回一個迭代器,該迭代器指向向量中的最后一個元素。
Program 1: Declare vector with Initialization and print the elements
程序1:使用初始化聲明矢量并打印元素
#include <iostream> #include <vector>using namespace std;int main() {// declare vector with 5 elements vector<int> num{10, 20, 30, 40, 50} ;//print the elements - to iterate the elements,//we need an iterator vector<int>::iterator it;//iterate and print the elementscout<< "vector (num) elements: ";for( it=num.begin(); it!=num.end() ; it++ )cout<< *it << " ";return 0; }Output
輸出量
vector (num) elements: 10 20 30 40 50 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Program 2: Declare a vector without initialization, insert some elements and print
程序2:在不初始化的情況下聲明向量,插入一些元素并打印
To insert elements in vector, we use vector::push_back() – this is a predefined function, it insert/pushes the elements at the end of the vector.
要在vector中插入元素,我們使用vector :: push_back() –這是一個預定義的函數(shù),它將在矢量末尾插入/推送元素。
#include <iostream> #include <vector>using namespace std;int main() {// declare vector vector<int> num{};//insert elementsnum.push_back (100);num.push_back (200);num.push_back (300);num.push_back (400);num.push_back (500);//print the elements - to iterate the elements,//we need an iteratorvector<int>::iterator it;//iterate and print the elementscout<< "vector (num) elements: ";for(it=num.begin (); it !=num.end (); it++ )cout<< *it << " ";return 0; }Output
輸出量
vector (num) elements: 100 200 300 400 500翻譯自: https://www.includehelp.com/stl/declare-initialize-and-access-a-vector.aspx
c++ stl隊列初始化
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的c++ stl队列初始化_声明,初始化和访问向量| C ++ STL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颐和园好玩吗值得去吗
- 下一篇: vc++ 6.0 堆栈_在C ++中使用