473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

vector of structs with vectors

I've got a gate structure that looks like this

/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};

And I have a vector of gates like this

/* Keeping GATE vector */
vector <sGATE> GATE;

Now, how do I write into name and <vector> input of a GATE? :-)

GATE.push_back(name=currentstring); //doesn't work
GATE.push_back(currentstring); //definitely doesn't work
GATE.push_back( ... err... what should i do? :-)

I know you can do something like

GATE.name.push_back(stuffhere) if gate was just a single thing. but
now it made my life a bit more complicated.

Could someone show me a direction here ?

come to think of it.. maybe i should make another struct and put it in
push_back() .. hmm.. let me try that while i post this. hopefully
there's a better way.

Sep 9 '05 #1
10 8443
Dear mashurshi,

On Fri, 2005-09-09 at 12:18 -0700, ma******@gmail.com wrote:
Now, how do I write into name and <vector> input of a GATE? :-)

GATE.push_back(name=currentstring); //doesn't work
GATE.push_back(currentstring); //definitely doesn't work
GATE.push_back( ... err... what should i do? :-)


vector<int> v(2,2); // or whatever you need for your vector
sGATE sg={"hoi",v,3};
GATE.push_back(sg);

Best wishes,
Chris

Sep 9 '05 #2
Well, what I can see, is, that you do not understand, what a vector is.
Try this:

sGate var;
// 1. item to fill in the vector
var.name.append("abc");
var.input.push_back(1);
....
GATE.push_back(var);
// 2. item to fill in the vector
var.name.append("xyz");
var.input.push_back(2);
....
GATE.push_back(var);
....
Hope, this helps.

// oliver

<ma******@gmail.com> schrieb im Newsbeitrag
news:11*********************@g43g2000cwa.googlegro ups.com...
I've got a gate structure that looks like this

/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};

And I have a vector of gates like this

/* Keeping GATE vector */
vector <sGATE> GATE;

Now, how do I write into name and <vector> input of a GATE? :-)

GATE.push_back(name=currentstring); //doesn't work
GATE.push_back(currentstring); //definitely doesn't work
GATE.push_back( ... err... what should i do? :-)

I know you can do something like

GATE.name.push_back(stuffhere) if gate was just a single thing. but
now it made my life a bit more complicated.

Could someone show me a direction here ?

come to think of it.. maybe i should make another struct and put it in
push_back() .. hmm.. let me try that while i post this. hopefully
there's a better way.

Sep 9 '05 #3
yeap. i made another variable of that struct and placed inside
push_back()

thanks for the quick replies folks.

Sep 9 '05 #4
<ma******@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I've got a gate structure that looks like this

/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};

And I have a vector of gates like this

/* Keeping GATE vector */
vector <sGATE> GATE;

Now, how do I write into name and <vector> input of a GATE? :-)

GATE.push_back(name=currentstring); //doesn't work
That's not C++ :)
GATE.push_back(currentstring); //definitely doesn't work
You can push_back an sGATE to GATE. By the way, some coding guidelines
reserve all-capital names for macros.
GATE.push_back( ... err... what should i do? :-)

I know you can do something like

GATE.name.push_back(stuffhere) if gate was just a single thing. but
now it made my life a bit more complicated.

Could someone show me a direction here ?


Are the inputs connected to the outputs of other gates? Then you may want to
keep a vector of int*. Would something like this work:

typedef vector<int const *> Inputs;

class Gate
{
string name_;
Inputs inputs_;
int output_;

public:

explicit Gate(string const & name);

void add_input(int const & input)
{
inputs_.push_back(&input);
}

int const & output() const;

/* ... */
};

int main()
{
vector<Gate> gates;
gates.push_back(Gate("one"));
gates.push_back(Gate("two"));
gates.push_back(Gate("three"));

int external_input_0 = 1;
int external_input_1 = 1;

// Connect some to external inputs
gates[0].add_input(external_input_0);
gates[1].add_input(external_input_1);

// Make further connections among gates
gates[2].add_input(gates[0].output());
gates[2].add_input(gates[1].output());

/* calculate */
}

Himmm... I am not sure where this is going... :)

If you are really going to connect outputs to inputs, as I suspect, then a
graph library like Boost's may help:

http://www.boost.org/libs/graph/doc/index.html

Ali

Sep 9 '05 #5
my knowledge in c++ is very limited (i just know the general
programming concepts and can do a bit of programming in a bunch of
languages... in other words, jack of all trades but a master of none..
i just write code for fun occasionally and learned stuff mostly by
myself, but thats it. so my programming style is taboo in the real
word :-) ).

i am doing this for a class (we need to make a very simple logic
simulator), the way we code it/language we use doesn't matter, just the
functionality matters. so i figured i should go with the simplest one
first. and if i get to the end and have time, maybe swith over to more
fancy stuff (just for the heck of learning, grade doesn't change
anyway)

i am having a very peculiar problem (i am posting my entire code here
to give a complete understanding of what's going on)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};

/* Keeping GATE/NET vectors global for now.. NEED TO CHANGE*/
vector <sGATE> GATE;
vector <int> NET;
sGATE CURRENTGATE;

int main(int argc, char *argv[])
{
/* Checking command line args */
if (argc < 2) { cout << "Please enter a filename\n" ; exit(1); }
if (argc > 2) { cout << "WARNING! Program only took first argument\n"
; }

/* Checking/Setting up input file */
ifstream infile(argv[1]);
if (!infile.is_open()) { cout << "ERROR! Can not access " << argv[1]
<< endl; exit(1); }

/* Reading the file contents into memory */
string currentword;

/* keep reading till the last word in the file */
while (infile >> currentword)
{
if (currentword == "INV" || currentword == "BUF")
{

CURRENTGATE.name = currentword;
cout << CURRENTGATE.name << " ";

/* copy the input and the output */
infile >> currentword;
CURRENTGATE.input.push_back(atoi(currentword.c_str ()));
cout << CURRENTGATE.input.at(0) << " ";

infile >> currentword;
CURRENTGATE.output = atoi(currentword.c_str());
cout << CURRENTGATE.output << endl;

/* copying the gate structure into the vector */
GATE.push_back(CURRENTGATE);
}
else if (currentword != "INPUT" && currentword != "OUTPUT")
{
CURRENTGATE.name = currentword;
cout << CURRENTGATE.name << " ";

/* copy the next two inputs and the output */
infile >> currentword;
CURRENTGATE.input.push_back(atoi(currentword.c_str ()));
cout << CURRENTGATE.input.at(0) << " ";

infile >> currentword;
CURRENTGATE.input.push_back(atoi(currentword.c_str ()));
cout << CURRENTGATE.input.at(1) << " ";

infile >> currentword;
CURRENTGATE.output = atoi(currentword.c_str());
cout << CURRENTGATE.output << endl;

GATE.push_back(CURRENTGATE);
}
else
{
/* need to handle INPUT and OUTPUT lines ending
with -1 at the end of the file */
}
}

/* Checking/Setting up output file */
string outputfilename = argv[1];
outputfilename = outputfilename + ".output";
ofstream outfile(outputfilename.c_str());

/* writing read input to outputfile */
for (int i = 0; i < GATE.size(); i++)
{
outfile << GATE.at(i).name << " " ;
// cout << "i is " << i << " " << GATE.at(i).name << " " ;
for (int j = 0; j < GATE.at(i).input.size(); j++)
{
outfile << GATE.at(i).input.at(j) << " ";
// cout << "j is " << j << " " << GATE.at(i).input.at(j) << " " ;
}
// cout << GATE.at(i).output << endl;
// cout << GATE.at(i).output << endl;
cout << "gate " << i << " inputs " << GATE.at(i).input.size() <<
endl;
}

return 0;
}

The input file (t.txt) looks like this
------------------------------------
AND 1 2 3
OR 2 3 4
INV 1 2
AND 4 5 6
BUF 5 7
Usage
--------
../a.out t.txt (in linux/unix)
yourexe.exe t.txt (in windows cmd line)
it should basically read the input file and tell me how many inputs
each gate has. (if i can get that right i can jump to other stuff
right :-) )

1. i checked it while it is reading the inputs (with those cout
statements, and it seems to work fine because i see pretty much
everything in the input file, which is good.)

2. while printing it out, i want it to figure out the length of the
input <vector> (in most cases, i know it is 2 and for BUF and INV it is
1.. but i want it to figure that out by itself) .. this isn't happening
:-(
i am thinking i screwed up somewhere in last the printing loop but i
couldn't get around this problem.

3. outfile is also not being written to properly, but i think i can
figure this out, i was spending my time investigating point 2 above.

any help is appreciated.

thanks.

Sep 10 '05 #6
i am probably the stupidest person you've ever come across in this
world.

i forgot to clear my input vector (after reading every gate).. aaahh
problem solved.

Sep 10 '05 #7
ma******@gmail.com wrote:
my knowledge in c++ is very limited (i just know the general
programming concepts and can do a bit of programming in a bunch of
languages... in other words, jack of all trades but a master of none..
i just write code for fun occasionally and learned stuff mostly by
myself, but thats it. so my programming style is taboo in the real
word :-) ).

i am doing this for a class (we need to make a very simple logic
simulator), the way we code it/language we use doesn't matter, just the
functionality matters. so i figured i should go with the simplest one
first. and if i get to the end and have time, maybe swith over to more
fancy stuff (just for the heck of learning, grade doesn't change
anyway)

i am having a very peculiar problem (i am posting my entire code here
to give a complete understanding of what's going on)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};

/* Keeping GATE/NET vectors global for now.. NEED TO CHANGE*/
vector <sGATE> GATE;
vector <int> NET;
sGATE CURRENTGATE;

int main(int argc, char *argv[])
{ [snip]

it should basically read the input file and tell me how many inputs
each gate has. (if i can get that right i can jump to other stuff
right :-) )

1. i checked it while it is reading the inputs (with those cout
statements, and it seems to work fine because i see pretty much
everything in the input file, which is good.)

2. while printing it out, i want it to figure out the length of the
input <vector> (in most cases, i know it is 2 and for BUF and INV it is
1.. but i want it to figure that out by itself) .. this isn't happening
:-(
i am thinking i screwed up somewhere in last the printing loop but i
couldn't get around this problem.

3. outfile is also not being written to properly, but i think i can
figure this out, i was spending my time investigating point 2 above.

any help is appreciated.

thanks.


Well one problem is the global variable CURRENTGATE. You've written
'NEED TO CHANGE', well change it now and your program will work.

The problem is that you are using the same CURRENTGATE each time. So
every time that you do CURRENTGATE.input.push_back(...)
CURRENTGATE.input keeps getting longer and longer and longer. You are
assuming that each time you start to read a new gate CURRENTGATE.input
is zero length but that isn't true.

Rewrite your code like this

if (currentword == "INV" || currentword == "BUF")
{
sGATE CURRENTGATE;
...
}
else if (currentword != "INPUT" && currentword != "OUTPUT")
{
sGATE CURRENTGATE;
...
}
else
{
...
}

that way you start with a new CURRENTGATE variable each time you read a
new gate.

And the moral is...?

Global variables are a bad thing, especially in the hands of a newbie.
Hope it's a lesson well learned.

john
Sep 10 '05 #8
ma******@gmail.com wrote:
i am probably the stupidest person you've ever come across in this
world.

i forgot to clear my input vector (after reading every gate).. aaahh
problem solved.


That work's, but it hides the real problem with you code, which is
inappropriate use of global variables.

john
Sep 10 '05 #9
thanks. i removed my .clear() statements and replaced them with sGATE
CURRENTGATE
the GATE and NET vectors are still global. i will change them once i
figure out how to pass in/return vectors to and from a function.
the thing is that GATE and NET vectors will be used by pretty much
every other function i am going to write in this code. i dont want to
keep adding to the overhead if i have to pass this in/out so many times
(although it really doesn't matter because my input files will have 500
gates max and you can do that fast on even vacuum tubes :-) )

i was wondering if it would be a good idea to somehow lock <vector>
GATE (so it can't be written to again) once i read it and filled it in
(so it can still be left as a global variable...) the NET needs to
keep changing though. all the other functions are gonna write to it.

any thoughts on this ?

Sep 10 '05 #10
ma******@gmail.com wrote:
thanks. i removed my .clear() statements and replaced them with sGATE
CURRENTGATE
the GATE and NET vectors are still global. i will change them once i
figure out how to pass in/return vectors to and from a function.
the thing is that GATE and NET vectors will be used by pretty much
every other function i am going to write in this code. i dont want to
keep adding to the overhead if i have to pass this in/out so many times
(although it really doesn't matter because my input files will have 500
gates max and you can do that fast on even vacuum tubes :-) )
If that is the case, and if you are sure that you will never need two
GATE vectors or two NET vectors then global variables aren't so bad.

i was wondering if it would be a good idea to somehow lock <vector>
GATE (so it can't be written to again) once i read it and filled it in
(so it can still be left as a global variable...) the NET needs to
keep changing though. all the other functions are gonna write to it.

OK well I think you're moving towards the idea of encapsulation, one of
the most important ideas in object orientated programming.

Suppose instead of having a global GATE vector you put that vector as a
private member of a class.

class GateVector
{
...
private:
vector <sGATE> GATE;
};

The important idea here is that the GateVector class can control what
can and can't be done to the GATE vector, in OO speak the GateVector
class encapsulates the operations that you want to do on a vector of
GATE's. So you could have

class GateVector
{
public:
void read_from_input(istream& input);
void write_to_output(ostream& output) const;
int number_of_gates() const;
string get_gate_name(int index) const;
private:
vector <sGATE> GATE;
};

I made up the different methods, they might not be what you actually
need, but the important thing is that this class expresses what you said
earlier. You said, after the gate array is read I want it to be locked,
well if you look at the methods I put in the GateVector class all of
them are declared const except the read_from_input one. So this class
expresses an important idea in your program, the only thing that ever
changes a gate vector is the read_from_input operation.

Incidentally, this class doesn't force you one way or another with
regard to whether the gate array is global or not. If you used a
GateVector class like this you could still have a global GateVector
variable, or not, as you think best.
any thoughts on this ?


john
Sep 10 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cheeser | last post by:
Hello, I'm trying to size a vector of vectors of unsigned ints to be an NxN square. Here's how I'm doing it: typedef vector<vector<unsigned int> > tournament_type; unsigned int n;
9
by: Nancy Keuss | last post by:
Hi, I've created a vector of vectors of ints, and I want to pass it as a parameter to a function. Is this possible, and if so, then what is the syntax like for the function header and function...
4
by: Creighton Hogg | last post by:
Hi, I'm having a problem that's driving me nuts right now. I wanted to represent data as a vector<vector<vector<unsigned short> > > Now I've been having alot of trouble initializing and...
9
by: Jeff | last post by:
Hello- Ive never used a vector or vectors in C++ so I have a question for you all. I know I can dynamically create the size I need upfront, but is it possible to create them on the fly...
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
0
by: acosgaya | last post by:
hi, is there an efficient for to insert the elements of a 2d array into a vector of vectors? I have vector< vector <int> > Points for (i = 0; i < n; i++ ) ...
4
by: foxx | last post by:
I have 2D data structure, modelled as a vector of vectors of ints. I'd like to visit each one of the ints and call a function on them. Is there some smart way of doing this without using a double...
2
by: foxx | last post by:
I have 2D data structure, modelled as a vector of vectors of ints. I'd like to visit each one of the ints and call a function on them. Is there some smart way of doing this without using a double...
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
4
by: Caudata | last post by:
I am by no means an experienced c++ programmer, but I am trying to use a vector of vectors because it is convenient to store some strings while parsing a text file. I am having trouble with the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.