Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ vector initialization

vector<int> a;                                       // empty vector of ints
vector<int> b (5, 10);                                // five ints with value 10
vector<int> c (b.begin(),b.end());                     // iterating through second
vector<int> d (c);                                   // copy of c
Comment

c++ vector initialization

#include <iostream>
#include <vector>
 
#define M 3
#define N 4
 
int main()
{
  // specify default value to fill the vector elements
  int default_value = 1;
  // first initialize a vector of ints with given default value
  std::vector<int> v(N, default_value);
  // Use above vector to initialize the two-dimensional vector
  std::vector<std::vector<int>> matrix(M, v);
 
  // This vector initializes with the values: 10, 20, and 30
  vector<int> vect{ 10, 20, 30 }; 
  
  return 0;
}
Comment

C++ Vector Initialization method 01

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

int main() {
    
    // initializer list
  vector<int> vect = {1, 2, 3, 4, 5};
  
  cout << "vector = ";

  // ranged loop
  for (const int& i : vect) {
    cout << i << "  ";
  }
  
  return 0;
}
Comment

C++ Vector Initialization method 02

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

int main() {
    
  // uniform initialization
  vector<int> vect{1, 2, 3, 4, 5};
  
  cout << "vector = ";

  // ranged loop
  for (const int& i : vect) {
    cout << i << "  ";
  }
  
  return 0;
}
Comment

C++ Vector Initialization method 03

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

int main() {
    
 // method 3
  vector<int> vect(5, 10);
  
  cout << "vector = ";

  // ranged loop
  for (const int& i : vect) {
    cout << i << "  ";
  }
  
  return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: free pair c++ 
Cpp :: numpy array scalar addition 
Cpp :: how to move your chrector in unity 
Cpp :: 400 watt hour per kg 
Cpp :: libraries required for gaming in c++ 
Cpp :: what is stdoutread in c++ 
Cpp :: c++ find unused class methods 
Cpp :: friend class in c++ 
Cpp :: Explicit conversion casting 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: Implement a currency converter which ask the user to enter value in Pak Rupees and convert in following: in cpp 
Cpp :: qt unhandled exception handler 
Cpp :: The Three Topics codechef solution in c++ 
Cpp :: can you use rand to read in from an external file inc++ 
Cpp :: c++ insert vector into vector 
Cpp :: 130 divided by -10 
Cpp :: std 
Cpp :: how to make a running text in c++ 
Cpp :: cplusplus 
Cpp :: sprintf add two xeroes for a float number 
Cpp :: sort 3 numbers using swap cpp 
Cpp :: Code Example of Switch Statement in C++/Java 
Cpp :: object as a function argument and returning object 
Cpp :: what is the format specifier for dword c++ 
Cpp :: round function in c++ 
Cpp :: scope resolution operator in c++ 
Cpp :: how to make a segment tree in c++ 
Cpp :: while loop c++ 
Cpp :: how to put string in array c++ 
Cpp :: qregexpvalidator qlineedit email address 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =