Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 12-04-2007, 08:20 AM   #1 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
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?
__________________
penguinyaro is offline   Reply With Quote
Old 12-04-2007, 08:21 AM   #2 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
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.
__________________
penguinyaro is offline   Reply With Quote
Old 12-05-2007, 12:29 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 635
DJMaze is on a distinguished road
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!
DJMaze is offline   Reply With Quote
Old 12-05-2007, 07:00 AM   #4 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
while I imagine that would be worlds easier. I'm not allowed to use structures.
__________________
penguinyaro is offline   Reply With Quote
Old 12-06-2007, 10:57 AM   #5 (permalink)
Kezzer
Recruit
 
Join Date: Nov 2007
Posts: 7
Kezzer is on a distinguished road
Send a message via Skype™ to Kezzer
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?
__________________
Kezzer is offline   Reply With Quote
Old 12-07-2007, 05:42 AM   #6 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
Probably not on the count of I don't know what you just said.
__________________
penguinyaro is offline   Reply With Quote
Old 12-07-2007, 05:59 AM   #7 (permalink)
Kezzer
Recruit
 
Join Date: Nov 2007
Posts: 7
Kezzer is on a distinguished road
Send a message via Skype™ to Kezzer
Are you allowed to use Object-Oriented programming? i.e. classes
__________________
Kezzer is offline   Reply With Quote
Old 12-07-2007, 08:25 AM   #8 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 635
DJMaze is on a distinguished road
C++ is all about OOP, how the hell could a teacher disallow you to use objects?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 12-07-2007, 10:29 AM   #9 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
It's an intro class. Object oriented is next class up.
__________________
penguinyaro is offline   Reply With Quote
Old 12-10-2007, 07:39 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
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!
__________________
Valmont is offline   Reply With Quote
Old 12-11-2007, 03:50 AM   #11 (permalink)
penguinyaro
Recruit
 
Join Date: Dec 2007
Posts: 6
penguinyaro is on a distinguished road
I'll check it out. Thanks for your all the help.
__________________
penguinyaro is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating and Using a 2 dimensional Vector (vector of vectors) xytor Standard C, C++ 2 09-12-2005 02:35 AM
C++ Multi Dimensional Array using Vectors. abs Standard C, C++ 16 02-26-2005 04:40 AM
A request from Ali regarding Back OS processes redhead Platform/API C++ 0 08-11-2004 01:48 AM
STL Vectors and what inside dmytro C++ 0 07-05-2004 12:16 AM


All times are GMT -8. The time now is 07:33 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle