943,685 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 908
  • C++ RSS
Aug 6th, 2009
0

vector of vectors

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. vector< vector<char> > set;
  2. vector<char> elements;
  3. char input = 'A';
  4.  
  5. set[input].push_back(elements);

but it doesnt work.
Last edited by JackDurden; Aug 6th, 2009 at 9:50 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Aug 6th, 2009
0

Re: vector of vectors

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
Last edited by daviddoria; Aug 6th, 2009 at 10:15 pm.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 6th, 2009
0

Re: vector of vectors

You are attempting to treat a <vector> as if it were a <set>, which is not possible.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. vector<vector<char>> vset;
  10. vector<char> element;
  11. vset.resize(255);
  12. element.push_back('a');
  13. element.push_back('b');
  14. element.push_back('c');
  15. vset['A'] = element;
  16. element[0] = 'e';
  17. element[1] = 'e';
  18. element[2] = 'f';
  19. vset['B'] = element;
  20.  
  21. vector<vector<char>>::iterator it1 = vset.begin();
  22. for( ; it1 != vset.end(); it1++)
  23. {
  24. if( !(*it1).empty() )
  25. {
  26. vector<char>::iterator it2 = (*it1).begin();
  27. for( ; it2 != (*it1).end(); it2++)
  28. {
  29. cout << *it2 << " ";
  30. }
  31. cout << "\n";
  32. }
  33. }
  34.  
  35.  
  36. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Aug 6th, 2009
0

Re: vector of vectors

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)
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4.  
  5. using namespace std;
  6.  
  7. class Set
  8. {
  9. public:
  10. int Menu_Screen();
  11. void Decision(int);
  12. void Create_Set(char);
  13. void Insert_element(char, char);
  14.  
  15. private:
  16. vector<vector<char>> list;
  17. vector<char> element;
  18. };
  19.  
  20. int Set::Menu_Screen()
  21. {
  22. int user_input;
  23.  
  24. cout<<"ENTER SET OPERATION\n";
  25.  
  26. cout<<"1 - Create_Set\n";
  27. cout<<"2 - Insert_element\n";
  28. cout<<"0 - EXIT\n";
  29. cout<<"ENTER: ";
  30. cin>>user_input;
  31.  
  32. return user_input;
  33. }
  34.  
  35. void Set::Decision(int num)
  36. {
  37. char set_name;
  38. char element;
  39.  
  40. switch(num)
  41. {
  42. case 1:
  43. cout<<"ENTER SET NAME: ";
  44. cin>>set_name;
  45. Create_Set(set_name);
  46. break;
  47. case 2:
  48. cout<<"ENTER SET NAME AND ELEMENT: ";
  49. cin>>set_name;
  50. cin>>element;
  51. Insert_element(element, set_name);
  52. break;
  53. case 0:
  54. cout<<"PROGRAM DONE\n";
  55. break;
  56. }
  57.  
  58. }
  59.  
  60. void Set::Create_Set(char set_name)
  61. {
  62. list[set_name].push_back(element);
  63. }
  64.  
  65. void Set::Insert_element(char ele, char Set)
  66. {
  67. //find set in vector
  68. //then insert ele into set
  69. }
  70.  
  71.  
  72. int main()
  73. {
  74. int input;
  75. Set start;
  76.  
  77. do
  78. {
  79. input = start.Menu_Screen();
  80. start.Decision(input);
  81.  
  82. }while(input != 0);
  83.  
  84. return 0;
  85. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Aug 6th, 2009
0

Re: vector of vectors

Again, you have not told us what is wrong or what you are expecting it to do. Is this an assignment? If not, why not just use std::set?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 6th, 2009
0

Re: vector of vectors

Thanks for the reply ancientdragon, that seems like something I can work with. Should I not be using vectors for this?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Aug 7th, 2009
0

Re: vector of vectors

vector<vector<char>> vset; //that should be an error because of the
confusion of the stream operator >>. Need space between them.
Last edited by firstPerson; Aug 7th, 2009 at 1:08 am.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 7th, 2009
0

Re: vector of vectors

vector<vector<char>> vset; //that should be an error because of the
confusion of the stream operator >>. Need space between them.
That might be true of older compilers. VC++ 2008 Express says its ok, but I do recall some compilers having problems with it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need the actual logic of beelow problem
Next Thread in C++ Forum Timeline: Enquiries about fstream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC