• Register
1 vote
19 views

Problem:

I am a newbie in the world of programming. I want to know the easiest way to initialize a vector in c++. If possible give me the codes too.

Thanks in advance

12 7 7
15,250 points

2 Answers

1 vote
 
Best answer

Solution:

Welcome to the fun world of programming, mate.

There are a couple of ways to initialize a vector in c++. As far as the easiest way is your concern I recommend you to use this way below.

#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Create an empty vector 
    vector<int> vect;   
    vect.push_back(5); 
    vect.push_back(10); 
    vect.push_back(20); 
  
    for (int n : vect) 
        cout << n << " "; 
  
    return 0;
}

or,

#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    vector<int> vect{ 5, 10, 20 }; 
  
    for (int n : vect) 
        cout << n << " "; 
  
    return 0; 
}

In both cases, your output should be 5,10, and 20.

13 9 6
94,240 points
0 votes

Solution:

One method would be to use the array to initialize the vector

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );

Initializing by pushing values one by one :

// CPP program to create an empty vector 
// and push values one by one. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// Create an empty vector 
	vector<int> vect; 
	
	vect.push_back(10); 
	vect.push_back(20); 
	vect.push_back(30); 

	for (int x : vect) 
		cout << x << " "; 

	return 0; 
} 

Specifying size and initializing all values :

// CPP program to create an empty vector 
// and push values one by one. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	int n = 3; 

	// Create a vector of size n with 
	// all values as 10. 
	vector<int> vect(n, 10); 

	for (int x : vect) 
		cout << x << " "; 

	return 0; 
} 

Initializing like arrays :

// CPP program to initialize a vector like 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	vector<int> vect{ 10, 20, 30 }; 

	for (int x : vect) 
		cout << x << " "; 

	return 0; 
} 

Initializing from an array :

// CPP program to initialize a vector from 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	int arr[] = { 10, 20, 30 }; 
	int n = sizeof(arr) / sizeof(arr[0]); 

	vector<int> vect(arr, arr + n); 

	for (int x : vect) 
		cout << x << " "; 

	return 0; 
} 

Initializing from another vector :

// CPP program to initialize a vector from 
// another vector. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	vector<int> vect1{ 10, 20, 30 }; 

	vector<int> vect2(vect1.begin(), vect1.end()); 

	for (int x : vect2) 
		cout << x << " "; 

	return 0; 
} 

 

10 6 4
31,120 points

Related questions

1 vote
1 answer 38 views
38 views
Problem: I have just completed reading the vector chapter from my c++ programming textbook. I completely understand how a vector works. I was about to start the exercise solving and got a problem says to initialize a 2d vector using c++ programming language, and that ... 2D vectors. Could you please help me to write the program with an example? I am looking forward to reading your answers. Thanks.
asked Jun 20 adamSw 11.3k points
0 votes
1 answer 13 views
13 views
I want to modify the length of a vector and at the same time eliminate the elements that remain if the new length is less than the original length of the vector. I don't know if there will be reserved words in the language to do this. It is with this type of vector: std :: vector name [];
asked Aug 17 game 4.6k points
0 votes
1 answer 139 views
139 views
I was making a C ++ program for entertainment which includes vectors. I need to get the size of the vector. Could someone help me to get said value?
asked Aug 15 game 4.6k points
0 votes
1 answer 9 views
9 views
Maximum and minimum in a C ++ vector with numbers from a file I'm trying to make a C ++ program that sends 100 random numbers to a file, then collects them and gets the largest number and the smallest number. I have tried to do this by putting the hundred numbers in a vector and taking the ...   printf ("The greater is% d \ n", greater);         printf ("The minor is% d \ n", minor);     } }
asked Aug 15 game 4.6k points
0 votes
1 answer 10 views
10 views
Well, this is a vector that tries to capture the letters, that is, each element of the vector is a letter that is being typed, however, in the end I don't know how to print the letters before typed. This is just one function. char enterChar () {      char character = '';      int i = 0;      if ...        cout << "element" << i << "is:" << phrase [i] << endl;      }      else          return 0; }
asked Aug 15 game 4.6k points