Answers for "cpp vector"

C++
36

c++ vector

#include <vector>

int main() {
  std::vector<int> v;
  v.push_back(10); // v = [10];
  v.push_back(20); // v = [10, 20];
  
  v.pop_back(); // v = [10];
  v.push_back(30); // v = [10, 30];
  
  auto it = v.begin();
  int x = *it; // x = 10;
  ++it;
  int y = *it; // y = 30
  ++it;
  bool is_end = it == v.end(); // is_end = true
  
  return 0;
}
Posted by: Guest on March-17-2020
1

c++ vector

#include <vector>
#include <string>

int main() {
  std::vector<std::string> str_v;
  str_v.push_back("abc");
  str_v.push_back("hello world!!");
  str_v.push_back("i'm a coder.");
  for(auto it = str_v.beigin();it != str_v.end(); it++) {
  	printf("%sn",it->c_str());
  }
}
Posted by: Guest on September-10-2021
1

how to use vectors c++

#include <iostream>
#include <vector>
using namespace std;

int main() {
  //vector element size
  const int size = 4; 
  //vector with int data type
  //all elements are equal to 4
  vector<int> myVect (size, 4);

  for (int i=0; i<size; i++) {
    cout << "Vector index(" << i <<") is: "<< myVect[i] << endl; 
  }
  return 0;
}
Posted by: Guest on October-31-2020
2

vector c++

Vectors are sequence container that can change size. Container is a objects 
that hold data of same type. Sequence containers store elements strictly in 
linear sequence.

Vector stores elements in contiguous memory locations and enables direct access
to any element using subscript operator []. Unlike array, vector can shrink or
expand as needed at run time. The storage of the vector is handled automatically.

To support shrink and expand functionality at runtime, vector container may 
allocate some extra storage to accommodate for possible growth thus container
have actual capacity greater than the size. Therefore, compared to array, vector
consumes more memory in exchange for the ability to manage storage and grow 
dynamically in an efficient way.

Zero sized vectors are also valid. In that case vector.begin() and vector.end()
points to same location. But behavior of calling front() or back() is undefined.
Posted by: Guest on May-13-2021
2

vector in c++

vector<int> g1; 
  
    for (int i = 1; i <= 5; i++) 
        g1.push_back(i); 
  
    cout << "Output of begin and end: "; 
    for (auto i = g1.begin(); i != g1.end(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of cbegin and cend: "; 
    for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
        cout << *i << " "; 
  
    cout << "nOutput of rbegin and rend: "; 
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
        cout << *ir << " ";
Posted by: Guest on February-02-2021
1

cpp vector structure

#include <vector>

typedef struct test1 {
  int a;
  char b;
} TOTO;

std::vector<TOTO> _v;

_v.push_back((TOTO){10, 'a'});
_v[0].a = 101;
Posted by: Guest on October-07-2020

Browse Popular Code Answers by Language