• Register
1 vote
24 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

Please log in or register to answer this question.

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 62 views
62 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, 2020 adamSw 11.3k points
0 votes
1 answer 2 views
2 views
Problem: I'm having trouble finding a solution; could you please assist me to solve how to initialize empty vector c++
asked 3 days ago ashik 12.9k points
0 votes
1 answer 15 views
15 views
Problem: I know in C++11 they added the feature to initialize a variable to zero as such double number = {}; // number = 0 int data{}; // data = 0 Is there a similar way to initialize a std::vector of a fixed length to all zero's?
asked Dec 26, 2020 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem: Hi! I am stuck in this problem if some one can help please ? Initialize vector of vectors c++ .
asked Mar 21 PkGuy 13.1k points
1 vote
1 answer 35 views
35 views
Problem: I am a pretty new student of c++ programming. I am learning the language from different websites and trying to solve the entry-level problems. Well, today I&rsquo;ve got a problem that says to use a vector of vectors in a c++ program. What does that ... question is pretty much straight forward. How do you construct a vector of vector in c++ programming? Please, shed some light on this.
asked May 29, 2020 adamSw 11.3k points
0 votes
1 answer 3 views
3 views
Problem: Hello there! I have heard about the structs in C++ but never heard about the structures in C. I want to know how do we declare and initialize a struct in C. I do not have prior experience in C but have worked a lot in C++, so some detailed explanation would be much appreciated.
asked Mar 24 Code Learner 9.2k points
0 votes
1 answer 3 views
3 views
Problem: I am stuck during learning as I have already mentioned in my question someone please cooperate me to continue my project-Thanks
asked Mar 24 Samiur1 38.4k points
0 votes
1 answer 2 views
2 views
Problem: How to initialize a object in constructor in C++?
asked Mar 30 jamuna1 18.1k points
0 votes
1 answer 1 view
1 view
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 24 sadi1982 36.3k points
0 votes
1 answer 7 views
7 views
Problem Hey, I know it is easy but please help me to learn how to initialise an array of objects with a parameterized constructor in c++.
asked Dec 12, 2020 chris jordan 2.4k points