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 :: how atan work c++ 
Cpp :: C++ file . 
Cpp :: HMC 5883 Example to return x y z values 
Cpp :: function for reversing an array c++ stl 
Cpp :: prefix using stack 
Cpp :: 41.00 
Cpp :: C++ Volume of a Cube 
Cpp :: OpenCV" is considered to be NOT FOUND 
Cpp :: check if a string is a prefix of another c++ 
Cpp :: convert hex to decimal arduino 
Cpp :: Minimizing the dot product codechef in c++ 
Cpp :: strcmp in c++ header file 
Cpp :: cpp how to add collisions to boxes 
Cpp :: 0-1 knapsack problem implementation of code input array 
Cpp :: . Shell sort in c++ 
Cpp :: C++ Display a text 5 times 
Cpp :: properties of loop in c++ and how it works 
Cpp :: codeforces problem 1700A solution in c++ 
Cpp :: sqrt function in c++ 
Cpp :: how to print out a two dimensional array in c++ 
Cpp :: what does npl mean? 
Cpp :: C++ singleton prevent copy 
Cpp :: error c4001 
Cpp :: c++ start process and get output 
Cpp :: new lien c++ 
Cpp :: second smallest element using single loop 
Cpp :: how to user input in string to open files in c++ 
Cpp :: c++ create a vecto 
Cpp :: cf 633b trivial problem explanation 
Cpp :: distructor of the node of the link list 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =