473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=currentstr ing); //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 8481
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=currentstr ing); //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******** *************@g 43g2000cwa.goog legroups.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=currentstr ing); //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******** *************@g 43g2000cwa.goog legroups.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=currentstr ing); //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_ba ck(&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(exte rnal_input_0);
gates[1].add_input(exte rnal_input_1);

// Make further connections among gates
gates[2].add_input(gate s[0].output());
gates[2].add_input(gate s[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_ope n()) { 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.nam e = currentword;
cout << CURRENTGATE.nam e << " ";

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

infile >> currentword;
CURRENTGATE.out put = atoi(currentwor d.c_str());
cout << CURRENTGATE.out put << endl;

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

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

infile >> currentword;
CURRENTGATE.inp ut.push_back(at oi(currentword. c_str()));
cout << CURRENTGATE.inp ut.at(1) << " ";

infile >> currentword;
CURRENTGATE.out put = atoi(currentwor d.c_str());
cout << CURRENTGATE.out put << 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(outputf ilename.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).inpu t.size(); j++)
{
outfile << GATE.at(i).inpu t.at(j) << " ";
// cout << "j is " << j << " " << GATE.at(i).inpu t.at(j) << " " ;
}
// cout << GATE.at(i).outp ut << endl;
// cout << GATE.at(i).outp ut << endl;
cout << "gate " << i << " inputs " << GATE.at(i).inpu t.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.inp ut.push_back(.. .)
CURRENTGATE.inp ut keeps getting longer and longer and longer. You are
assuming that each time you start to read a new gate CURRENTGATE.inp ut
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

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

Similar topics

3
8513
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
5947
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 prototype when I need to specify the variable type? Thank you, N.
4
3174
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 accessing elements for that type. I can't seem to initialize like blah(10,10,10) to make each vector length 10, so instead I did blah(10,(10,10)) but even
9
15909
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 dynamically? That is, for a 2 dim array, I want to create the first element and then push_back the columns after that:
0
2144
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 the vector of vectors according to the median value of one the dimensions. I tried to use the stl partition method like this: // S is a vector of vectors containing the points vector < vector <int> >::iterator iter;
0
1666
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++ ) Points.push_back(vector<int>(d));
4
2748
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 for loop,? I mean how could I go about creating a new kind of iterator that knows how to transverse all the ints in some sequence; or better still, does STL already have such a feature?
2
4197
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 for loop,? I mean how could I go about creating a new kind of iterator that knows how to transverse all the ints in some sequence; or better still, does STL already have such a feature?
3
2887
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 relies on the PrintFunctor to traverse the "inner" vector with another for_each. Is it possible to nest the second for_each, so I don't have to include a for_each in my function object. Is it possible to do a: for_each(myVec.begin(), myVec.end(),
4
4395
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 nested for loop recovery of the stored data and properly dereferencing the data. Here is a bit of test code: #include <iostream> #include <string> #include <vector> using namespace std; struct s { string strA;
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.