data structure using c++

Discussion in 'C++' started by coolroose, Mar 9, 2012.

  1. coolroose

    coolroose New Member

    Joined:
    Nov 7, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone help me with this? pleaseeeeeeeeeeeee
    I really need help with this program: data structure using c++:


    All sequence containers have some common member functions. The list container provides some additional functions such as: push_front, pop_front, remove, remove_if, reverse, sort among others.

    Write an App that uses the STL list container to store the strings: "Hammer", "Screw driver", "Copper Pipe", and "Ladder". Then add to the front of the list the string "Bulbs" and to the end of the list the strings "Wrench" and "Ladder". Display the list. Sort the list by calling the sort function. To use the sort function you have to include the <algorithm> header file. Call the unique() function on the list to delete consecutive elements. Display the list.
     
  2. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    Code:
    /*
     * Write an App that uses the STL list container to store the strings:
     * "Hammer", "Screw driver", "Copper Pipe", and "Ladder". Then add to 
     * the front of the list the string "Bulbs" and to the end of the list 
     * the strings "Wrench" and "Ladder". Display the list. Sort the list 
     * by calling the sort function. To use the sort function you have to
     * include the <algorithm> header file. Call the unique() function on 
     * the list to delete consecutive elements. Display the list.
     */
    
    /* 
     * Includes
     */
    #include <iostream>
    #include <list>
    #include <string>
    
    
    void UsingList()
    {	
    	using std::list;
    	using std::string;
    	using std::cout;
    	using std::endl;
    
    	list<string> myStrList;
    	myStrList.push_front(string("Hammer"));
    	myStrList.push_front("Screw driver");
    	myStrList.push_front("Copper Pipe");
    	myStrList.push_front("Ladder");
    
    	myStrList.push_front("Bulbs");
    
    	myStrList.push_back("Wrench");
    	myStrList.push_back("Ladder");
    
    	cout << "Original List:" << endl;
    	unsigned int index = 0;
    	for(auto it = myStrList.begin(); it != myStrList.end(); ++it) {
    		cout << "myStrList[" << index++ << "] = " << *it << endl;
    	}
    
    	myStrList.sort([](const string& left, const string& right)->bool{
    		if(left.compare(right) >= 0)
    			return false;
    		else
    			return true;
    	});
    
    	cout << "\n\nSorted List:" << endl;
    	index = 0;
    	for(auto it = myStrList.begin(); it != myStrList.end(); ++it) {
    		cout << "myStrList[" << index++ << "] = " << *it << endl;
    	}
    
    	myStrList.unique([](const string& left, const string& right)->bool{
    		if(left.compare(right) == 0)
    			return true;
    		else
    			return false;
    	});
    
    	cout << "\nUnique List:" << endl;
    	index = 0;
    	for(auto it = myStrList.begin(); it != myStrList.end(); ++it) {
    		cout << "myStrList[" << index++ << "] = " << *it << endl;
    	}
    }
    
    int main(int argc, char* argv[])
    {
    	UsingList();
    	
    	return 0;
    }
    
    Tested and compiled on Visual Studio 2010. which support lamda expression new feature of c++0x
     
  3. coolroose

    coolroose New Member

    Joined:
    Nov 7, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    can you explain this line code please from the program above stl container:

    ([](const string& left, const string& right)->bool{
    if(left.compare(right) == 0)
    return true;
    else
    return false;
    });

    I have commented this code and just use "myStrList.sort(); it runs just fine

    thanks,
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice