returning a pointer to a STL list object

Started by
3 comments, last by rom7L 19 years, 7 months ago
my program creates a graph from names in a text file by creating a linked list of nodes that have a (string) name. i have a function that serches through the nodes list to find out if the node is already in the list. the function is passed a string (called key), which is the name of the node it is looking for. if it finds the node, it returns a pointer to the node (node*). the search function uses an iterator to search through the nodes list (nList). however, i haven't been able to sucsessfully return a pointer to the node, i jsut get garbage. i think this is because the iterator is creating a copy of the node, which is only relative to the scope of the search function. when the address is returned to the caller, it is garbage. here's what it looks like:

//the node struct and nodeList class
struct node
{
	string name;
	list<node*> edgeList;
        node(string nodeName)
	   {
		   name = nodeName;
	   }
};

class nodeList 
{
private: 
	list<node> nList;

public:
        void graph();
	void printGraph();
	node* search(string);
};


//graph function - makes the call to search()
void nodeList::graph()
{   
	node* keyPtr = NULL;
	node* valuePtr = NULL;
   	string key, value;
        ifstream iFile("test.txt");
   
	//read file, get key and value
	while (!iFile.eof()) 
	   {   
	      iFile >> key >> value;
	      //find key in node list, set pointer to key, set pointer to edgeList
		  keyPtr = search(key);
		
		  //if key not found, add that node to the list 
                  if (keyPtr == NULL)
		     {
				 nList.push_back(node(key));
				 keyPtr = &nList.back();
		     }

		  //search node list for value, if 'value' not in list, add it
		 valuePtr = search(value);
                 if (valuePtr == NULL)
		    {
				nList.push_back(node(value));
				valuePtr = &nList.back();
	            }

		 keyPtr->edgeList.push_back(valuePtr);
	   }
}



//search function
node* nodeList::search(string key)
{   
	//int count=0;
	//node* temp = NULL;
	typedef list<node>::iterator ListIterator;
	for (ListIterator l = nList.begin(); l != nList.end(); l++) 
	   {
		   if ( stricmp( l->name.c_str(), key.c_str()) == 0 )
		      {

				 return (node*)&l
			  }
			
	   }
   return NULL;
}
i'm thinking of reimplementing the node list by using map<> instead of list<>
Advertisement
change &l to l

the iterator IS the pointer, for all practical purposes.
A lot of weird things your doing, why are you using stricmp when string has an overloaded equality operator? also you do relize STL containers & iterators return constant & non-constant references to the elements? also you don't need to write a search function you can use STL algorithms std::find_if with a predicate.

I know you've posted before about this, i don't know if you saw my post, i recommended you use STL multimap for what your trying to do.

Quote:Original post by C-Junkie
change &l to l

the iterator IS the pointer, for all practical purposes.


The way he has it thats not going to work he needs to "deference" the iterator which returns a reference to the element, if he really needs to return a pointer he needs to take the address aswell e.g.

    return &(*l);
Don't do this. An iterator might be a pointer for all practical purposes, but it doesn't have to be only a pointer. There is no guarantee casting will work.

What does work is: return &(*it);
@snk_kid: yeah, i saw your post, and that would work, but i wanted to use list because it doesnt do any sorting.

but yes, you and trap are right. using &(*l) does exactly what i want it to do. this rocks, thanks!

This topic is closed to new replies.

Advertisement