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:

vector< vector<char> > set;
	vector<char> elements;
    char input = 'A';

	set[input].push_back(elements);

but it doesnt work.

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

You are attempting to treat a <vector> as if it were a <set>, which is not possible.

#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";
        }
    }


}

Ok so here is some more code, it has the idea of what i want to do with my program.

#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;
}

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?

Thanks for the reply ancientdragon, that seems like something I can work with. Should I not be using vectors for this?

vector<vector<char>> vset; //that should be an error because of the
confusion of the stream operator >>. Need space between them.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.