| | |
vector of vectors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
I was wondering if you guys might help me figure out how to make a vector of vectors like so,
The user will name the set say 'A'. And then using 'A' as a key you can then find the set name and add say 'a', 'b' to it. How can I do this with a vector of vectors? Ive tried things like this:
but it doesnt work.
The user will name the set say 'A'. And then using 'A' as a key you can then find the set name and add say 'a', 'b' to it. How can I do this with a vector of vectors? Ive tried things like this:
C++ Syntax (Toggle Plain Text)
vector< vector<char> > set; vector<char> elements; char input = 'A'; set[input].push_back(elements);
but it doesnt work.
Last edited by JackDurden; Aug 6th, 2009 at 9:50 pm.
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
You'll have to post a bit more complete code. What is 'input'? Why is 'elements' empty?
Please give us an example input, the current (incorrect) output, and the expected output.
Also, maybe std::string is better suited for what you're trying to do?
Dave
Please give us an example input, the current (incorrect) output, and the expected output.
Also, maybe std::string is better suited for what you're trying to do?
Dave
Last edited by daviddoria; Aug 6th, 2009 at 10:15 pm.
You are attempting to treat a <vector> as if it were a <set>, which is not possible.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<vector<char>> vset; vector<char> element; vset.resize(255); element.push_back('a'); element.push_back('b'); element.push_back('c'); vset['A'] = element; element[0] = 'e'; element[1] = 'e'; element[2] = 'f'; vset['B'] = element; vector<vector<char>>::iterator it1 = vset.begin(); for( ; it1 != vset.end(); it1++) { if( !(*it1).empty() ) { vector<char>::iterator it2 = (*it1).begin(); for( ; it2 != (*it1).end(); it2++) { cout << *it2 << " "; } cout << "\n"; } } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Ok so here is some more code, it has the idea of what i want to do with my program.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> #include<vector> using namespace std; class Set { public: int Menu_Screen(); void Decision(int); void Create_Set(char); void Insert_element(char, char); private: vector<vector<char>> list; vector<char> element; }; int Set::Menu_Screen() { int user_input; cout<<"ENTER SET OPERATION\n"; cout<<"1 - Create_Set\n"; cout<<"2 - Insert_element\n"; cout<<"0 - EXIT\n"; cout<<"ENTER: "; cin>>user_input; return user_input; } void Set::Decision(int num) { char set_name; char element; switch(num) { case 1: cout<<"ENTER SET NAME: "; cin>>set_name; Create_Set(set_name); break; case 2: cout<<"ENTER SET NAME AND ELEMENT: "; cin>>set_name; cin>>element; Insert_element(element, set_name); break; case 0: cout<<"PROGRAM DONE\n"; break; } } void Set::Create_Set(char set_name) { list[set_name].push_back(element); } void Set::Insert_element(char ele, char Set) { //find set in vector //then insert ele into set } int main() { int input; Set start; do { input = start.Menu_Screen(); start.Decision(input); }while(input != 0); return 0; }
![]() |
Similar Threads
- GUI, matrix and vector libraries (C++)
- Vector of vectors & magic square issues (C++)
- problem inserting elements in a 2D vector (C++)
- how can i use an iterator for a 2d vector? (C++)
- elements common to more than one vector (C)
Other Threads in the C++ Forum
- Previous Thread: Need the actual logic of beelow problem
- Next Thread: Enquiries about fstream
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






