View Full Version : C++ how to initialize a vector
erotavlas
May 9th, 2011, 11:09 AM
Hi,
I have a simple question. How can I initialize the following vector?
std::vector<std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > > myVector;
I would like to have 5 elements (for example) inside of myVector. i.e. the outer vector of myVector should be of size 5.
I think that the following way is not the only one.
std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > tempVector(0);
for (int i = 0; i < 5; ++i)
{
myVector.push_back(tempVector);
}
What are the other ways?
Thank you
ziekfiguur
May 9th, 2011, 12:41 PM
You could use the constructor: std::vector(size_type n, const T& value= T(), const Allocator& = Allocator());
It initializes the vector with its content set to a repetition, n times, of copies of value.
Just one remark though, i think it would be better to define a class than to use std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > as a datatype. It would certainly make your code a lot more readable and easier to understand. If you don't want to do that, you could also define a typedef for it so you don't have to repeat the whole thing every time in your code.
erotavlas
May 9th, 2011, 01:44 PM
You could use the constructor: std::vector(size_type n, const T& value= T(), const Allocator& = Allocator());
It initializes the vector with its content set to a repetition, n times, of copies of value.
Just one remark though, i think it would be better to define a class than to use std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > as a datatype. It would certainly make your code a lot more readable and easier to understand. If you don't want to do that, you could also define a typedef for it so you don't have to repeat the whole thing every time in your code.
Thank you for your fast answer. Can you tell me how I can do your solution?
Can I use my code without using the tempVector?
std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > tempVector(0);
for (int i = 0; i < 5; ++i)
{
myVector.push_back(tempVector); // myVector.push_back(0) ????
}
ziekfiguur
May 9th, 2011, 02:04 PM
since that constructor uses T() as a default argument i think you should just be able to use:
std::vector<std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > > myVector(5);
i haven't tested it though, maybe you would have to use
..blahblah... myVector(5, tempVector);
dwhitney67
May 9th, 2011, 02:15 PM
Is this not easier to digest, that is, clearer to read?
struct Data
{
std::string str;
int val;
};
struct Data2
{
std::vector<Data> data;
double dbl;
};
std::vector<Data2> myVector;
If you want to initialize the 'myVector' to have 5 elements:
std::vector<Data2> myVector(5);
nvteighen
May 9th, 2011, 04:25 PM
dwhitney67's solution points to a general approach you should always take in account when programming: repeated code is usually a symptom of insufficient/incorrect abstraction. This is valid in all programming languages, no matter what paradigm; of course the solutions may vary from one language to another, but the general idea is always the same: avoid repetitions by creating new concepts that allow further simplification of your code.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.