Answers for "vector size in c++"

C++
13

c++ vector size

#include <vector>

int main() {
  std::vector<int> myVector = { 666, 1337, 420 };
  
  size_t size = myVector.size(); // 3
  
  myVector.push_back(399); // Add 399 to the end of the vector
  
  size = myVector.size(); // 4
}
Posted by: Guest on May-16-2020
1

declare vectors c++

vector<int> vec;
//Creates an empty (size 0) vector
 

vector<int> vec(4);
//Creates a vector with 4 elements.

/*Each element is initialised to zero.
If this were a vector of strings, each
string would be empty. */

vector<int> vec(4, 42);

/*Creates a vector with 4 elements.
Each element is initialised to 42. */


vector<int> vec(4, 42);
vector<int> vec2(vec);

/*The second line creates a new vector, copying each element from the
vec into vec2. */
Posted by: Guest on May-25-2020
0

create a vector of size n in c++

// create a vector with n integer elements
std::vector<int> arr(n);
// create a vector with n integer elements initalized at 0 
std::vector<int> vector1(n, 0);
Posted by: Guest on November-20-2021
1

find vector size in c++

vectorName.size();
Posted by: Guest on March-12-2021
-1

c++ create vector of size

// create a vector with 20 integer elements
std::vector<int> arr(20);

for(int x = 0; x < 20; ++x)
   arr[x] = x;
Posted by: Guest on July-15-2020
-1

declare vector of size n in c++

#include <vector>

auto n = 20
// create a vector with n=20 integer elements
std::vector<int> arr(n);
Posted by: Guest on September-13-2020

Browse Popular Code Answers by Language