|
 |
|
 |
 |
12-04-2007, 08:20 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
Pushing back into 2 dimensional vectors
How do you do it in C++?
This is what I assumed:
Code:
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream getData;
vector<string> year;
vector< vector<string> > title;
vector< vector<double> > gross;
vector< vector<string> > lastname;
vector< vector<string> > firstname;
vector< vector<int> > age;
vector< vector<char> > type;
vector<int> counter(7, 0); // 0 = L, 1 = Y, 2 = T, 3 = G, 4 = A, 5 = Q, 6 = bad input
int i, j, n;
string yearin;
string titlein;
double grossin;
string lastnamein, firstnamein;
int agein;
char typein;
bool done = false, done1 = false;
char input;
i = 0;
getData.open("moviescs105.txt");
while (!done1)
{
getData >> yearin;
if (yearin == "ENDOFFILE")
{
done1 = true;
}
else
{
year.push_back(yearin);
getData >> n;
for (j = 0; j > n; j++)
{
getData.ignore(200,'\n');
getline(getData, titlein);
title[i].push_back(titlein);
getData >> grossin;
gross[i].push_back(grossin);
getData >> lastnamein;
lastname[i].push_back(lastnamein);
getData >> firstnamein;
firstname[i].push_back(firstnamein);
getData >> agein;
age[i].push_back(agein);
getData >> typein;
type[i].push_back(typein);
}
getData.ignore(200,'\n');
i++;
}
}
getData.close();
pushing back into 1 d vector works fine. But I don't knwo how to do it for 2 dimensional ones. Am I supposed to use an itterator or something? And if so how?
__________________
|
|
|
12-04-2007, 08:21 AM
|
#2 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
by the way
Code:
2007
5
Spider Man 3
336.53 Maguire Tobey 32 a
Shrek the Third
321.01 theMonster Shrek 573 c
Transformers
319.03 Fox Megan 21 d
Pirates of the Caribbean: At World's End
309.42 Depp Johnny 43 c
Harry Potter and the Order of the Phoenix
291.71 Radcliffe Daniel 19 d
2006
4
Pirates of the Caribbean: Dead Man's Chest
423.32 Depp Johnny 43 c
Night at the Museum
250.86 Stiller Ben 42 c
Cars
244.08 McQueen Lightning 37 c
X-Men: The Last Stand
234.36 Berry Halle 41 d
2005
3
Star Wars: Episode III - Revenge of the Sith
380.27 Portman Natalie 36 d
The Chronicles of Narnia: The Lion, the Witch, and the Wardrobe
291.71 Henley Georgie 12 a
Harry Potter and the Goblet of Fire
290.01 Radcliffe Daniel 19 a
1997
2
Titanic
600.79 DiCaprio Leonardo 33 d
Men in Black
250.69 Smith Will 39 c
ENDOFFILE
is the file I am streaming in.
__________________
|
|
|
12-05-2007, 12:29 AM
|
#3 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 635
|
Wouldn't it be easier to have a structure?
Code:
typedef struct {
string year;
string title;
double gross;
string lastname;
string firstname;
int age;
char type;
int counter; // 0 = L, 1 = Y, 2 = T, 3 = G, 4 = A, 5 = Q, 6 = bad input
} PersonData;
__________________

UT: Ultra-kill... God like!
|
|
|
12-05-2007, 07:00 AM
|
#4 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
while I imagine that would be worlds easier. I'm not allowed to use structures.
__________________
|
|
|
12-06-2007, 10:57 AM
|
#5 (permalink)
|
|
Recruit
Join Date: Nov 2007
Posts: 7
|
You could just create a new class containing all the necessary fields and instantiate objects from that. Then you could have
vector<myContainer> containerObjects;
Are you allowed OO code?
__________________
|
|
|
12-07-2007, 05:42 AM
|
#6 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
Probably not on the count of I don't know what you just said.
__________________
|
|
|
12-07-2007, 05:59 AM
|
#7 (permalink)
|
|
Recruit
Join Date: Nov 2007
Posts: 7
|
Are you allowed to use Object-Oriented programming? i.e. classes
__________________
|
|
|
12-07-2007, 08:25 AM
|
#8 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 635
|
C++ is all about OOP, how the hell could a teacher disallow you to use objects?
__________________

UT: Ultra-kill... God like!
|
|
|
12-07-2007, 10:29 AM
|
#9 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
It's an intro class. Object oriented is next class up.
__________________
|
|
|
12-10-2007, 07:39 AM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Peng, welcome on our boards!
For your question, use the "search" button for 2d array's (or something similar). You'll find easy-to-understand examples, although all the functions are wrapped in a class. But observe the functions (methods we call them) and you'll have nice hints (free giveaways practically).
Like this idea?
Good luck!
__________________
|
|
|
12-11-2007, 03:50 AM
|
#11 (permalink)
|
|
Recruit
Join Date: Dec 2007
Posts: 6
|
I'll check it out. Thanks for your all the help.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:51 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|