Reputation: 155
I would rather use a class, but my requirement is simpler than that. Which is to create a two dimensional array of elements, with each element representing a structure datatype. The problem is when I try to do a variable initialisation after the multi vector is passed into a function. The simple version of my code is as follows:
#include <iostream>
#include <vector>
using namespace std;
struct Twalker
{
int state;
float x,y;
};`
void initialise(vector<vector<Twalker>>& walker,int n)
{
vector<Twalker> tempWalker;
for (int i=0; i<n; i++)
{
tempWalker.state.push_back(+1); //need help!
tempWalker.y.push_back(float(rand() % 10)); //need help!
tempWalker.x.push_back(0.0); //need help!
}
walker.push_back(tempWalker);
}
int main(int argc, char** argv)
{
int n = 5;
vector<vector<Twalker>> walker;
initialise(walker,n);
}
I have seen that we can add multiple values into a structure using push_back
. But i was trying to input values more liberally. Did I miss any syntax while using push_back
? I started learning the concept of vectors few hours before and I am trying to shift my code from arrays
into vectors
.
Upvotes: 0
Views: 114
Reputation: 4744
First of all, structures are equivalent to classes - the only difference is that their members are by default public, while class members are by default private.
Your program has multiple issues, and here is one possible solution to it:
#include <iostream>
#include <vector>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
struct Twalker
{
int state;
float x,y;
};
// Initialize the vector of vectors of Twalker objects
void initialise(vector<vector<Twalker>>& walker, int nv, int n)
{
Twalker wlk;
vector<Twalker> tempWalker;
// Outer loop - vector of vectors
for (int i=0; i<nv; i++){
// Inner loop - vector of Twalker objects
for (int j=0; j<n; j++){
// Temporary Twalker
wlk.state = 1;
wlk.x = 0.0;
wlk.y = float(rand()%10);
// Add it to the inner vector
tempWalker.push_back(wlk);
}
// Add the inner vector to the vector of vectors
walker.push_back(tempWalker);
// Reset the inner vector
tempWalker.clear();
}
}
int main()
{
// Set the seed
std::srand(std::time(0));
int nv = 3, n = 2;
vector<vector<Twalker>> walker;
// Initialize
initialise(walker, nv, n);
// Print
for (int i = 0; i < nv; i++)
for (int j = 0; j < n; j++)
cout << walker[i][j].state << " "
<< walker[i][j].x << " "
<< walker[i][j].y << endl;
return 0;
}
You should definitely read more on vectors including basics and concepts like iterators and size_t
. Here I only used variations of what it seems you are familiar with, but some parts of this program would normally look quite different (like printing part).
Some notes and suggestions related to this code:
using namespace std
. You may, for instance, end up overwriting stl functions with your own by accident. It is more recommended to use using
for specific, commonly used components like cin
, cout
, vector
etc.nv
times longer than the first!srand
function resets the random seed every time the program is ran. Without it, executing the same program would always lead to the same set of random numbers. argc
and argv
only when I have command line arguments in the program. Also, you can but don't have to add a return 0
statement at the end of your main (compiler will add it for you if you don't).tempWalker
after the inner loop, you can also declare it in the inner loop which will create a new, empty tempWalker
vector each time the inner loop is re-entered. On the other hand, as noted by @MatteoItalia, this approach allocates and deallocates memory for the inner vector each time the inner loop is executed. In larger programs with larger vectors this kind of redundant allocations will cause performance losses and should be avoided.Upvotes: 1