i want too create a 2-D vector of Boolean type with Size n x n . but i don't want to use two for loop to assign false to every index instead of assign directly while creating this vector.
syntax is:- vector<vector<bool>> V(n,false)
but it gives me error
There is a constructor that takes a count and an element, but false is not a std::vector<bool>. You want
std::vector<std::vector<bool>> V(n, std::vector<bool>(n,false) );
// |---- inner vectors ----|
Two disclaimer:
Use std::vector<bool> with caution. It is not like other std::vectors (see eg Alternative to vector<bool>).
A vector of vectors is rarely the best data structure. The power of a std::vector is its data locality, however a std::vector<std::vector<T>> does not have this feature (only elements of the inner vectors are contiguous).
vector<vector> V(n,false) this gives an error because you are not declaring size of each v[i] thus, it can not assign each of v[i][j] to false.
you can use vector<vector> v(n, vector (n, false));
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<bool>> v(n, vector<bool> (n, false));
for(int i=0;i<n;i++)
{
for(int j=0;j<v[i].size();j++)
{
cout<<v[i][j]<<" ";
}
cout<<'\n';
}
}
Ans:
n=5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Related
I just started using c++, so I am still new to vectors and data structures. I'm wondering how to I add a int value into a specific index location of 2d vector. For example, I have a multidimensional vector "chessboard" initialized like:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
How do I use the insert() method to insert a integer value 1 into say the x,y index [4,3] so It looks like this:
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
This Is my code so far:
vector<vector<int>> board(boardsize, vector<int> (boardsize, 0));
int x = 4; // x position for value to be inserted
int y = 3; // y position for value to be inserted
In order to change a value of an element of a std::vector, you can use std::vector::operator[].
For example in your case of a vector containing vectors:
board[x][y] = 1;
Note that std::vector::insert does not modify an existing element, but rather adds a new element (or elements) to the vector. This is not what you want since you set the final desired size of the vector upon construction.
A side note: better to avoid using namespace std; - see here: Why is "using namespace std;" considered bad practice?.
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<vector<int> > matrix;
matrix.resize(3, vector<int>(4, 1));
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 4; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
matrix.resize(5, vector<int>(7, 0));
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 7; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
return 0;
}
'''
As far as I know, when we are resizing a vector using "resize()" over than original capacity, values in original space will remain and new values are assigned to new space.
In the line matrix.resize(5, vector(7, 0)); If we execute that line I thought matrix would be like
1111000
1111000
1111000
0000000
0000000
something like this.
But the programs stops,
I want to know why it won't working.
matrix.resize(5, vector<int>(7, 0));
only add new vector of size 7 not modifying actual vector.
Just resize actual vectors to 7 with:
for (auto &row: matrix) row.resize(7);
so now is working:
1111000
1111000
1111000
0000000
0000000
i tested your codes using an online compiler https://onlinegdb.com/BkwcGuAAD
your columns are not resized (only the rows are resized). running your current code yields
1 1 1 1 0 0 33
1 1 1 1 0 0 33
1 1 1 1 0 0 49 << notice some columns have random numbers?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
try resizing the columns too
matrix[0].resize(7,0);
matrix[1].resize(7,0);
matrix[2].resize(7,0);
matrix.resize(5, vector<int>(7, 0));
you should get something like the following
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
By the C++ Reference
(http://www.cplusplus.com/reference/vector/vector/resize/)
void resize (size_type n);
void resize (size_type n, const value_type& val);
Resizes the container so that it contains n elements.
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
I thought Compiler will understand if I put more longer vector inside of "val".
That is, I thought it would understand this kind of increased "n".
But Compiler will only watch whether "n" parameter itself is changed or not.
Because of this reason, my code wouldn't work properly.
if you want to increase size of vector using size() function, note that you should resize original row values on your hand.
I would like to generate a matrix in C ++ using armadillo that behaves like a "truth table", for example:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
I was thinking of a cycle of this kind, but I'm not very practical with armadillo and its data structures.
imat A = zeros<imat>(8, 3);
/* fill each row */
for(int i=0; i < 8; i++)
{
A.row(i) = (i/(pow(2, i)))%2 * ones<ivec>(3).t(); //
}
cout << "A = \n" << A << endl;
Any ideas?
If you need a large size truth table matrix (~2^30 x 30) as you said here, from the memory point of view, you should implement a function which quickly calculates the values you want rather than storing them on a matrix.
This is easily done using std::bitset as follows.
Note that N must be determined at compile-time in this method.
Then you can get the value of your A(i,j) by matrix<3>(i,j):
DEMO
#include <bitset>
template <std::size_t N>
std::size_t matrix(std::size_t i, std::size_t j)
{
return std::bitset<N>(i)[N-j-1];
}
Why is the vector 'r' giving the output as follows? Instead, it should not have zero in the list.
Can someone help?
output: 0 0 0 0 0 5 1 2 3 4
vector <int> leftRotation(vector <int> a, int d) {
vector<int> r(a.size());
// int j=0;
for(int i=d; i<a.size(); i++)
r.push_back(a[i]);
for(int i=0; i<d; i++)
r.push_back(a[i]);
return r;
}
You are initializing r with a.size() copies of 0 at the start of your code:
vector<int> r(a.size());
At this point, the vector r contains values 0 0 0 0 0 (as many zeroes as there are elements in a).
Then, you push values onto the back of this vector of zeroes. At each step of your loops:
0 0 0 0 0 5
0 0 0 0 0 5 1
0 0 0 0 0 5 1 2
0 0 0 0 0 5 1 2 3
0 0 0 0 0 5 1 2 3 4
Instead, initialize with an empty vector:
vector<int> r;
The local variable of vector<int> r(a.size()) uses a vector's constructor overload that accepts (size_type count) argument and is initialized (or rather zeroed out) to contain a.size() number of 0s. For clarification check out the std::vector constructor overload no. 3:
The overload (3) zeroes out elements of non-class types such as int
Use a default constructor to create an empty container instead:
std::vector<int> r;
or:
std::vector<int> r{};
vector<int> r(a.size());
With this line, you create a vector of initial size a.size(); the initial elements in the vector are set to 0.
Probably your a vector has size 5, so this is where the five zeros in your output come from:
0 0 0 0 0
Then, you invoke the push_back() method on r, and this appends other elements at the end of the r vector.
To get a better help, please clarify your goal.
I'm working on a basic image editor using vectors and I'm trying to get a matrix like the one shown below, a 4 by 4 but with each slot containing 3 values (RGB values)
0 0 0 100 0 0 0 0 0 255 0 255
0 0 0 0 255 175 0 0 0 0 0 0
0 0 0 0 0 0 0 15 175 0 0 0
255 0 255 0 0 0 0 0 0 255 255 255
So far I've been able to get a matrix put in with this code (I think..)
int rows;
int columns;
fin >> rows;
fin >> columns;
vector<vector<int> > image_dimensions(rows);
for (int i = 0; i < rows; i++)
image_dimensions[i].resize(columns);
This is only to create a matrix full of 0s. How can I make each slot contain three RGB values like the example showed? Finally, what complicated loop would I have to use to be able to change the individual values in each slot, like turning 0 0 0 into 155 0 255 for example?
At the moment you use the primitive integer type. Consider to store some more complex like a RGB class:
class RGB {
private:
std::unit8_t r, g, b;
public:
RGB() {}
RGB(int r, int g, int b) : r(r), g(g), b(b){ }
}
You can simple construct objects with:
rgb = RGB(255,255,255);
After that you can store the objects in your vector.
int rows;
int columns;
fin >> rows;
fin >> columns;
vector<vector<RGB> > image_dimensions(rows);
for (int i = 0; i < rows; i++)
image_dimensions[i].resize(columns);
The difference to your example is, that the vector can store objects from the class RGB. If you like you can now iterate through the vector:
for (int i = 0; i < columns; i++)
for (int i = 0; i < rows; i++)
image_dimensions[i][j] = RGB(0,0,0);
edit:
based on the comment, it is probably better to not waste space and use the the uint8 type. So there is no more need for the verification.
And you could use a flat vector and calculate the 2d index. If you work with big matrices this will be generate a more efficient solution.
edit2:
A second possible solution without creating a new class is to use the std tuple class:
std::tuple<std::unit8_t, std::unit8_t, std::unit8_t > rgb(0,0,0);
In this case your vector have to store this kind of tuple.
More tuple informations: klick
By the way: creating a little class or structure should not disturb you. ;-)