#1
So, in my programming class, we are using C++. Its object oriented programming now. So, I need to output a table using parallel vectors. I cannot simply "hard code" it. It is in a for loop, which is already set up. I know im being vague. But if anyone who knows code can help, Id appreciate it.
Spread Hope Like Fire


Angels and Airwaves


Et Ducit Mundum Per Lucem
#2


lol wut?

Edit: Seriously, I have no idea what you are on about though, maybe someone will, maybe you will get a straight answer try comput(er)ing thread?
Quote by lambofgod127
btw im in hs and im almost 18 so if u do think she was flirting with me dont say that its wrong im almost a grown man.




༼ ▀̿Ĺ̯▀̿ ̿ ༽ WE ARE ROB ༼ ▀̿Ĺ̯▀̿ ̿ ༽
Last edited by slash_GNR666 at Apr 12, 2011,
#3
Maybe get help here;
http://www.cplusplus.com/

It's a great site, and there's a forum.
Sunn O))):
Quote by Doppelgänger
You could always just sleep beside your refrigerator.

Guitar:
- Ibanez S670FM w/ JB
- Fender 'Lite Ash' Stratocaster
- Fender '72 Deluxe Telecaster
- Arbiter LP Jr. Doublecut
Amp:
- Laney VC15

'72 Tele Appreciation Group
RIP DIO
#4
So, I have 4 parallel apvectors. And variables with the data in them. I need to output the data in the variables in a loop, using the vectors.

And there is a computer thread?
Spread Hope Like Fire


Angels and Airwaves


Et Ducit Mundum Per Lucem
#6
neither, im just confused.
Spread Hope Like Fire


Angels and Airwaves


Et Ducit Mundum Per Lucem
#7
You output the information from vectors just as how you would output them from arrays. If it's in the for loop, you simply use loop control variable to shift through the index of the array. The only reason a vector is unique is because it is an invoked template, but it is to be used just like an array.

Ex:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> myVar(4, 10); //Creates vector myVar, and sets 4 int memory spaces to 10

for(int x = 0; x<myVar.size(); x++)
{
cout<<myVar[x];
}

return 0;
}

You also get some functions to help out with getting information regarding the array throughout the program. Also, the constructor enables you to specify some other forms of parameters (like setting it to another vector) regarding the vector that's being initialized.

EDIT: It should be x<myVar.size() and NOT x<=myVar.size(). The latter will output garage data for the 5th value.
Last edited by FaisalTMusic at Apr 12, 2011,
#8
wouldn't that just be a case of

for(int i = 0; i < vector1.length; i++)
cout >> vector1 >> vector2;

or something like that? Sorry I don't write a lot of C++ so a lot to that syntax is probably going to be wrong and you're obviously gonna have to tailor the output to your requirements.

This is really simple... I feel like I'm probably missing something so let me know if I am
#9
personally I would use an iterator


vector<template>::iterator it; //declare an iterator for your type of vector

  cout << "myvector contains:";
  for ( it=myvector.begin() ; it < myvector.end(); it++ ) //loop through vector
    cout << " " << *it; //output vector data

"I can see that we speak the wrong notes."

Warlock NJ Deluxe (haters gonna hate)
Crate EL-10G
#10
o_0

lol wut?
You want some more seeneyj hate? WELL YOU CAN'T HAVE IT

You're all a bunch of f*cking slaves! - Jim Morrison

UG Awards
1st: Biggest Ego
1st: Most Likely To Become Famous
1st: Most Pretentious User
#11
What kind of parallel vectors and what format is the table?

personally I would use an iterator

You're overcomplicating it. He's using vectors, which overload the subscript [] operator. He also said he's using parallel vectors. If he has to, for example, print the item in each index of each vector on a single line then he would have to use multiple iterators.
#12
I'm sorry for being really unconstructive here, but I find it amazing that some of the dudes here in the Pit actually know how to code. It's cool. xD
SATCH FTW!!!
NSW Group FTW!

There's nothing incredibly interesting here.
#13
Quote by satanicgurrl
personally I would use an iterator


vector<template>::iterator it; //declare an iterator for your type of vector

cout << "myvector contains:";
for ( it=myvector.begin() ; it < myvector.end(); it++ ) //loop through vector
cout << " " << *it; //output vector data



This. iterators are the most effective way to run through data kept in vectors. Just remember that you need to use a pointer to the iterator to print/manipulate the data. This method can then be used constantly without having to change it (which is quite handy) even if you add data to the vector using myvector.push_back(data)

This is particularly useful in large codes. It means you can declare a function that will print all of your data, especially if your using class hierarchy.eg
class abstracclass{ //abstract base class
protected:
public:
     virtual void print()=0; //virtual function, so it can override for everything further on.
};

class myclass: public abstractclass{ //now this class will inherit all of the base class data also
protected:
   vector<template> myvector;
   vector<template>::iterator current;
   virtual void print(){
      cout << "myvector contains: " << endl;
      for ( current=myvector.begin() ; it < myvector.end(); current++ ) //loop through vector
      cout << " " << *it << endl;; //output vector data
   }
};

class secondclass: myclass{ //each layer of class now inherits the print function, meaning it only needs typing once, and can be used as much as needed
etc
};


edit: just added a couple things....
Last edited by l3vity at Apr 12, 2011,
#14
Well, it looks like I need to explain more. I have 4 parallel apvectors. Also, some variables. I have all that declared and initialized. for loop is set up. I need to output in the for loop. I dont have access to the code, or access to Visual Studio. I appreciate your answers.
Spread Hope Like Fire


Angels and Airwaves


Et Ducit Mundum Per Lucem
#15
Quote by jakesc8
Well, it looks like I need to explain more. I have 4 parallel apvectors. Also, some variables. I have all that declared and initialized. for loop is set up. I need to output in the for loop. I dont have access to the code, or access to Visual Studio. I appreciate your answers.

I don't see what the problem is then.

Just run a simple for loop like:


for (size_t index = 0; index < //size of vectors; index++)
{
     cout << vector1[index] << '\t' << vector2[index] << '\t' << vector3[index] << 't'
          << vector4[index] << '\n';
}
#16
alright. Ill try it. I have that class tomorrow. Thanks. Ill let you guys know.
Spread Hope Like Fire


Angels and Airwaves


Et Ducit Mundum Per Lucem