Initialzing a 2d vector in C++ - c++

I know that you can initialize a 1d vector with some value in the following way:
vector<int> vec(10, 100); //creates a vector of size 10 and each element = 100
Now I would like to do the same thing with a 2d vector. I know the following would give an error, because the size of columns is not specified:
vector<vector<int> > vec(10, 100);
So, is there any way to accomplish this? Also, I want to keep the column size same for each vector in the 2d vector (i.e., an nxn grid).
Or maybe I can use the "std::fill()" function in some way to accomplish this? And can this functionality be extended to an nxm grid?

vector<vector<int>> vec(10, vector<int>(10, 100));
// n m value
This will create a vector of 10 vectors of size 10 and with elements initialized to 100.

Related

3 dimensional vector in c++

i want to store a vector of objects for a node and subset of set.
so I want to implement a 3 d vector where the first dimension has size #nodes = n and the second dimension has size 2^|set| = m. the last dimension shouldn't have fixed size, because I want to append new objects in my program.
vector< vector < vector<Object>>> Vector3d( n , ( m, ( ( ???)))
what should I write instead of ???
i have tried to use arrays inside of a vector, but I didn't succeed that way.
thank you in advance
The method of initialisation is
vector< vector < vector<Object>>> Vector3d(n,vector<vector<Object>(m))
This will work, as it will initialize the first dimension's size as n, which would contain n no of vector<vector<Object>(m) which is the default value. Now the size of the second dimension is defined as m, which has no initial value, hence you can append any vector to the second dimension space.
i.e.
vector<Object> v;
Vector3d[i][j].push_back(v);
where i and j are the indexes of the first 2 dimensions

How to push queue into a vector of queues when vector size is not constant? [duplicate]

In some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?
std::vector has a fill constructor which creates a vector of n elements and fills with the value specified. a has the type std::vector<std::vector<int>> which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0) which means each row has a vector which has cols number of element, each being 0.
For declaring a 2D vector we have to first define a 1D array of size equal to number of rows of the desired 2D vector.
Let we want to create a vector of k rows and m columns
"vector<vector<int>> track(k);"
This will create a vector of size k. Then use resize method.
for (int i = 0; i < k; i++) {
track[i].resize(m);
In this way you can declare a 2D vector

Declaring a 2D vector

In some cases only the below line works.Why so?
vector< vector<int>> a(M,N);
This works in every case.
vector< vector<int>> a(M, vector<int> (N));
What's the difference?
std::vector has a fill constructor which creates a vector of n elements and fills with the value specified. a has the type std::vector<std::vector<int>> which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0) which means each row has a vector which has cols number of element, each being 0.
For declaring a 2D vector we have to first define a 1D array of size equal to number of rows of the desired 2D vector.
Let we want to create a vector of k rows and m columns
"vector<vector<int>> track(k);"
This will create a vector of size k. Then use resize method.
for (int i = 0; i < k; i++) {
track[i].resize(m);
In this way you can declare a 2D vector

How to define 2D vector which rows are known c++ [duplicate]

This question already has answers here:
allocating vectors (or vectors of vectors) dynamically
(9 answers)
Closed 8 years ago.
what is the best way to define 2D vector, which rows are known? So basically, I will have something like this:
2 3 4
7 5 4 12 4
2 1 0 2
I will know how many rows there are (well actuall I will know it only after getting row variable), but each row collumn will vary. So what is the best way to do this? (I am planning on pushing back each row's columns when I will need, if it is possible)
Here is someone who had a similar question http://www.cplusplus.com/forum/beginner/12409/. Just Define a vector of vectors.
It wont let me comment down below, but in your code you have tiesiogiaiJungiasi[A.at(i)]
What type is A and if it is a vector then is that vector filled with integers if not A.at(i) is your problem. That will give you the element that is at that location not the location itself.
If you are trying to push the element index i from row B into the element index i of row A try something like this.
tiesiogiaiJungiasi[A].push_back(tiesiogiaiJungiasi[B][i]);
assuming there is an element in row B at the index location.
Another problem could be that you say that your rows may be of different sizes. Well you should look at your m parameter in the for loop then. If that number is larger than the number of columns (elements) in that row i has the possibility of walking off the array.
If i'm understanding correctly, this seems like what you want is an array of vectors. You can set an array of vectors like vector<string> myArray[3]; and then push values into myArray[0], myArray[1], etc.
If I understood correctly. You want a data structure that will allow you to append to the end of each row. A vector< vector < int > > will do.
vector< vector < int > > rows;
You have to populate this, if you know the max number of rows you could write:
vector< vector < int > > rows(MAX);
You append to the end of a row using rows[i].push_back(...);. Rows are numbered starting at 0 (add 1 to MAX if you want to start at 1).

C++ - read 1000 floats and insert them into a vector of size 10 by keeping the lowest 10 numbers only

So I am pretty new to c++ and I am not sure if there is a data structure already created to facilitate what I am trying to do (so I do not reinvent the wheel):
What I am trying to do
I am reading a file where I need to parse the file, do some calculations on every floating value on every row of the file, and return the top 10 results from the file in ascending order.
What am I trying to optimize
I am dealing with a 1k file and a 1.9 million row file so for each row, I will get a result that is of size 72 so in 1k row, I will need to allocate a vector of 72000 elements and for the 1.9 million rows ... well you get the idea.
What I have so far
I am currently working with a vector for the results which then I sort and resize it to 10.
const unsigned int vector_space = circularVector.size()*72;
//vector for the results
std::vector<ResultType> results;
results.reserve(vector_space);
but this is extremely inefficient.
*What I want to accomplish *
I want to only keep a vector of size 10, and whenever I perform a calculation, I will simply insert the value into the vector and remove the largest floating point that was in the vector, thus maintaining the top 10 results in ascending order.
Is there a structure already in c++ that will have such behavior?
Thanks!
EDIT: Changed to use the 10 lowest elements rather than the highest elements as the question now makes clear which is required
You can use a std::vector of 10 elements as a max heap, in which the elements are partially sorted such that the first element always contains the maximum value. Note that the following is all untested, but hopefully it should get you started.
// Create an empty vector to hold the highest values
std::vector<ResultType> results;
// Iterate over the first 10 entries in the file and put the results in the vector
for (... ; i < 10; i++) {
// Calculate the value of this row
ResultType r = ....
// Add it to the vector
results.push_back(r);
}
// Now that the vector is "full", turn it into a heap
std::make_heap(results.begin(), results.end());
// Iterate over all the remaining rows, adding values which are lower than the
// current maximum
for (i = 10; .....) {
// Calculate the value for this row
ResultType r = ....
// Compare it to the max element in the heap
if (r < results.front()) {
// Add the new element to the vector
results.push_back(r);
// Move the existing minimum to the back and "re-heapify" the rest
std::pop_heap(results.begin(), results.end());
// Remove the last element from the vector
results.pop_back();
}
}
// Finally, sort the results to put them all in order
// (using sort_heap just because we can)
std::sort_heap(results.begin(), results.end());
Yes. What you want is a priority queue or heap, defined so as to remove the lowest value. You just need to do such a remove if the size after the insertion is greater than 10. You should be able to do this with STL classes.
Just use std::set to do that, since in std::set all values are sorted from min to max.
void insert_value(std::set<ResultType>& myset, const ResultType& value){
myset.insert(value);
int limit = 10;
if(myset.size() > limit){
myset.erase(myset.begin());
}
}
I think MaxHeap will work for this problem.
1- Create a max heap of size 10.
2- Fill the heap with 10 elements for the first time.
3- For 11th element check it with the largest element i.e root/element at 0th index.
4- If 11th element is smaller; replace the root node with 11th element and heapify again.
Repeat the same steps until the whole file is parsed.