how Iterate through a vector of lists

Hey, guys. I'm trying to figure this out. I have a implemented hash table, a vector of lists of pointers to objects, and I'm trying "dump", i.e. go through and print the whole table. I'm just not sure how to iterate through the vector until a list is found, then iterate through the list and print each object and print each object. Any help is very appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  template <class T>
class hashTable{

public:
	hashTable(){};
	~hashTable(){};
	void setSize(int size);
	void toInsert(T toAdd);
	void toDelete(std::string name);
	void Print(std::string name);
	void dump();
	int hashFunc(std::string name);
	friend ostream& operator<<(ostream& os, const T& RHS);


private:

	vector< list<T> > table;
	int tableSize;
	int alpha;



};


This is not working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vector<list<T> >::iterator start, stop;
	start = table.begin();
	stop = table.end();
	list<T>::iterator begin, end;

	while(start != stop){
	
		if((*start) != -99){							//compiler error 
		
			begin = table[//iterator index?].begin();	//compiler error
			end = table[//iterator index?].end();
				while(begin != end){
				
					(*begin)->print();
					begin++;
				}
		}
	
	
		start++;
	}


I can locate a particular node and print
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
void hashTable<T>::Print(std::string name){

	int hashKey = hashFunc(name);
    list<T>::iterator start, stop; 
	start = table[hashKey].begin();
	stop = table[hashKey].end();

	while(start != stop && (*start)->getName() != name){
		start++;
	}
	if(start != stop){
		(*start)->print();
	}
	else{
		cout<<"not found"<<endl;
	}

}
nevermind guys i figured it out. thanks anyway.

This is a working solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <class T>
void hashTable<T>::dump(){
	
	
	list<T>::iterator begin, end;

	for(int i = 0; i < tableSize;i++){
	
		if(table[i].empty() == false){
			begin = table[i].begin();
			end = table[i].end();
			while(begin != end){
			
				(*begin)->print();
				begin++;
			
		
		}
		}
	
	
		
	}
}
You don't need to check if the list is empty, if it is begin() == end().
C++03:
1
2
3
4
5
6
7
8
for(vector<list<T> >::iterator it = table.begin(); it != table.end(); ++it)
	{
		for(list<T>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
		{
			cout << *it2 << ", ";
		}
		cout << endl;
	}

C++11:
1
2
3
4
5
6
7
8
for(auto& lst : table)
	{
		for(auto i : lst)
		{
			cout << i << ", ";
		}
		cout << endl;
	}
Last edited on
Hmm thanks I did not think about that.

I actually have another problem, sort of on this same topic.

I need to overload the extraction operator<< for the template class above, and print that iterator, problem is the list item it's pointing to is a pointer to an object.

any ideas?
The stream operator you have right now doesn't make sense for the hashTable, you won't know what type T is so you cant overload the stream operator for T. For my examples above T needs to have operator<< overloaded. If you want something like
1
2
3
hash_table h;
...
cout << h;
Then the operator declaration should look like friend ostream& operator<<(ostream& os, const hashTable& RHS); with a possible implementation of
1
2
3
4
5
6
7
8
9
10
11
ostream& operator<<(ostream& os, const hashTable& RHS)
{
	for(auto& lst : RHS.table)
	{
		for(auto i : lst)
		{
			os << i << ", ";
		}
		os << endl;
	}
}
since the table is a template class its going to require an argument list. The above that I have is incorrect.

should be something like:
1
2
template <class T>
friend ostream& operator<<(ostream& os, const hashTable<T>& RHS);


i need to be able to use dump() to iterate through the table and use << to print each object stored. The lists themselves hold a pointer ( the type is of an abstract base class) to object (derived class)
Topic archived. No new replies allowed.