Given the following code snippet:
vector<vector<int>> *Vec_2D = new vector<vector<int>>;
vector<int> int_list; // declaration and header info on stack
Vec_2D.push_back(int_list);
for(int i=0; i < 10; i++) {
(*Vec_2D)[0].push_back(i); // load some stuff into first array of Vec_2d
}
I declareint_list on the stack, and then push_backinto Vec_2D on the heap. Header information for int_list is now on stack; I know the elems of int_list are on heap. This seems wasteful.
Can this be avoided? Something like:
vector<vector<int>> *Vec_2D = new vector<vector<int>>;
Vec_2D.push_back(new vector<int>); // avoid declaration on stack
for(int i=0; i < 10; i++) {
(*Vec_2D)[0].push_back(i); // load some stuff into first array of Vec_2d
}
Is this syntax possible, vector::push_back(new myVec)? This way I avoid the decl. on stack, and the header information of the vector on the stack referred to by int_list.
Cheers!!
You could use emplace_back:
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> *Vec_2D = new vector<vector<int>>;
Vec_2D->emplace_back(vector<int>{}); // avoid declaration on stack
for (int i=0; i < 10; i++) {
(*Vec_2D)[0].push_back(i); // load some stuff into first array of Vec_2d
}
return 0;
}
i am working on a code which increases size of diagonal elements by 1
here is the working implementation
consider 5x5 matrix
#include<iostream>
using namespace std;
struct abc
{
int b[100];
}arr[100];
int main()
{
for(int i=0;i<5;i++)
arr[i].b[i]+=1;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cout<<arr[i].b[j]<<" ";
cout<<endl;
}
//system("pause");
return 0;
}
The code works fine for small range of arr,but i need it to work for 10^5.Any Suggestions?
I think its better to perform such memory allocations on the heap.
I have the following code, where i defined a vector of vector of struct
#include <vector>
#include <iostream>
using namespace std;
struct node
{
int index;
double value;
};
int main()
{
vector < vector <node> >vett1;
node p;
p.index=5;
p.value=2;
for (int i=0; i<10; i++)
vett1[i].push_back(p);
return 0;
}
i don't know the right way to fill it. In this way when i run it, compilers gives me segmentation fault error.
When you access vett1[i], but the vett1 has not been filled with size zero. That's why the segmentation fault error occur.
Three ways to fix it:
Add
vett1.resize(10);
before the for loop.
Or define vett1 and set its size as follows:
vector <vector <node>> vett1(10);
Or you can do this if you don't know the exact size pre-hand:
for (int i=0; i<10; i++)
{
vector<node> temp;
temp.push_back(p);
vett1.push_back(temp);
}
This code gives a compile-error on Topcoder. On code::blocks, it compiles with 0 errors and 0 warnings, the vector is printed, but it exits with a non-zero value, that causes windows to display "InterestingDigits.exe has stopped working". Any help on this..?
#include<iostream>
#include<vector>
using namespace std;
class InterestingDigits
{
public:
vector <int> digits(int base)
{
vector<int> v;
for(int i=2; i<base; i++)
if(base%i==1)
v.push_back(i);
for(int i=0; i<v.size(); ++i)
cout<<v[i]<<" ";
cout<<endl;
}
};
int main()
{
int base;
cin>>base;
InterestingDigits id;
id.digits(base);
return 0;
}
It seems that your method digits() should return a value, a vector < int >.
Maybe adding a "return v;" at the end?
You need to return a vector. With your code, compiler would try to return something that caused the undefined behaviour. So adding "return v" should fix your issue.
#include <iostream>
using namespace std;
class SomeClass {
public:
bool someArray[4][4]={{0,0,0,0},{0,0,0,0}};
};
int main()
{
SomeClass super;
super.someArray={{1,1,1,0},{1,0,0,1}}; //This goes red, indicates a mistake. How do i properly fill it?
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
Please see the comments in the code above.
By the way: super.someArray[4][4]={{1,1,1,0},{1,0,0,1}}; doesn't work either and it probably shouldn't.
You probably mean to use bool someArray[2][4] (i.e, an array with two elements that contains arrays with four boolean elements).
You can't assign one array into another in C++; you'll need to copy the individual elements. I.e., something like:
super.someArray[0][0] = 1;
super.someArray[0][1] = 1;
super.someArray[0][2] = 1;
super.someArray[0][3] = 0;
super.someArray[1][0] = 1;
super.someArray[1][1] = 0;
super.someArray[1][2] = 0;
super.someArray[1][3] = 1;
(If you have some source for your data, you could use a loop of course.)
The following worked for me using the GNU compiler. Notice that I replaced your raw array with std::tr1::array. This class is more flexible with respect to assigning entire arrays (as opposed to just initializing arrays from literals).
#include <iostream>
#include <tr1/array>
using namespace std;
using namespace tr1;
typedef array<array<bool,4>,4> array4x4;
class SomeClass {
public:
array4x4 someArray;
SomeClass() : someArray((array4x4){{{{0,0,0,0}},{{0,0,0,0}}}}) {}
};
int main()
{
SomeClass super;
super.someArray=(array4x4){{{{1,1,1,0}},{{1,0,0,1}}}}; //Now works
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
However, the following approach is a bit closer to where you started, and demonstrates some of the things suggested in other comments...
#include <iostream>
#include <algorithm>
using namespace std;
class SomeClass {
public:
bool someArray[4][4];
SomeClass()
{
bool temp[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for ( int j=0; j<4; j++ ) for ( int i=0; i<4; i++ ) someArray[j][i] = temp[j][i];
}
};
int main()
{
SomeClass super;
bool temp[4][4] = {{1,1,1,0},{1,0,0,1}}; // a local source of data
for ( int j=0; j<4; j++ ) for ( int i=0; i<4; i++ ) super.someArray[j][i] = temp[j][i];
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
super.someArray[4][4]={{1,1,1,0},{1,0,0,1}};
The line above just needs to be:
super.someArray[4][4]={1,1,1,0,1,0,0,1};
Explaination:
it will automatically go the next section of the array. If you think of it as a table, once the first row is filled up, it will start declaring it for the next row.
So if you wrote:
super.someArray[4][4]={1,1,1,1,1};
it would set:
someArray[0][0]
someArray[0][1]
someArray[0][2]
someArray[0][3]
someArray[1][0]
all equal to 1.
(I might have the numbers switched so it could be x and y places are changed, I can't recall for c++)