Connecting Tech Pros Worldwide Forums | Help | Site Map

vector of structs with vectors

mahurshi@gmail.com
Guest
 
Posts: n/a
#1: Sep 9 '05
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.


Chris Dams
Guest
 
Posts: n/a
#2: Sep 9 '05

re: vector of structs with vectors


Dear mashurshi,

On Fri, 2005-09-09 at 12:18 -0700, mahurshi@gmail.com wrote:
[color=blue]
> 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? :-)[/color]

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

Best wishes,
Chris

Oliver \(Nospam\)
Guest
 
Posts: n/a
#3: Sep 9 '05

re: vector of structs with vectors


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

<mahurshi@gmail.com> schrieb im Newsbeitrag
news:1126293498.648210.96900@g43g2000cwa.googlegro ups.com...[color=blue]
> 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.
>[/color]


mahurshi@gmail.com
Guest
 
Posts: n/a
#4: Sep 9 '05

re: vector of structs with vectors


yeap. i made another variable of that struct and placed inside
push_back()

thanks for the quick replies folks.

Ali Çehreli
Guest
 
Posts: n/a
#5: Sep 9 '05

re: vector of structs with vectors


<mahurshi@gmail.com> wrote in message
news:1126293498.648210.96900@g43g2000cwa.googlegro ups.com...[color=blue]
> 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[/color]

That's not C++ :)
[color=blue]
> GATE.push_back(currentstring); //definitely doesn't work[/color]

You can push_back an sGATE to GATE. By the way, some coding guidelines
reserve all-capital names for macros.
[color=blue]
> 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 ?[/color]

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

mahurshi@gmail.com
Guest
 
Posts: n/a
#6: Sep 10 '05

re: vector of structs with vectors


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.

mahurshi@gmail.com
Guest
 
Posts: n/a
#7: Sep 10 '05

re: vector of structs with vectors


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.

John Harrison
Guest
 
Posts: n/a
#8: Sep 10 '05

re: vector of structs with vectors


mahurshi@gmail.com wrote:[color=blue]
> 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[])
> {[/color]
[snip]
[color=blue]
>
> 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.
>[/color]

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
John Harrison
Guest
 
Posts: n/a
#9: Sep 10 '05

re: vector of structs with vectors


mahurshi@gmail.com wrote:[color=blue]
> 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.
>[/color]

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

john
mahurshi@gmail.com
Guest
 
Posts: n/a
#10: Sep 10 '05

re: vector of structs with vectors


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 ?

John Harrison
Guest
 
Posts: n/a
#11: Sep 10 '05

re: vector of structs with vectors


mahurshi@gmail.com wrote:[color=blue]
> 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 :-) )[/color]

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.
[color=blue]
>
> 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.
>[/color]

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.
[color=blue]
> any thoughts on this ?
>[/color]

john
Closed Thread