vector of vectors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

vector of vectors

 
0
  #1
Aug 6th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: vector of vectors

 
0
  #2
Aug 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector of vectors

 
0
  #3
Aug 6th, 2009
You are attempting to treat a <vector> as if it were a <set>, which is not possible.
  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. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: vector of vectors

 
0
  #4
Aug 6th, 2009
Ok so here is some more code, it has the idea of what i want to do with my program.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: vector of vectors

 
0
  #5
Aug 6th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: vector of vectors

 
0
  #6
Aug 6th, 2009
Thanks for the reply ancientdragon, that seems like something I can work with. Should I not be using vectors for this?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,259
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: vector of vectors

 
0
  #7
Aug 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector of vectors

 
0
  #8
Aug 7th, 2009
Originally Posted by firstPerson View Post
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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC