• Register
1 vote
21 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 44 views
44 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 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 5.7k points
0 votes
1 answer 18 views
18 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, 2020 game 4.6k points
0 votes
1 answer 394 views
394 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, 2020 game 4.6k points
0 votes
1 answer 15 views
15 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, 2020 game 4.6k points
0 votes
1 answer 2 views
2 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.2k points
1 vote
1 answer 29 views
29 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 15 views
15 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, 2020 game 4.6k points
1 vote
2 answers 670 views
670 views
Problem: Hello Kodlogs, Myself is a beginner level student of c++ and I am trying to solve problems from a website. I was doing good until I got a problem that says, c++ read file line by line into vector. I was looking for a solution over the internet but I failed. Could you please solve the problem?
asked Apr 3, 2020 Gavin 15.3k points
0 votes
1 answer 27 views
27 views
I need to find the index of a number in the vector in C ++, particularly the minimum of the same array. I want to do the same as, for example, in Python, it does the .index () method on a python list, but with a vector in C ++. Example in python of what I want to do in C ++: arr.index (min (arr))
asked Aug 6, 2020 amna 52.5k points