Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   data structure using c++ (http://www.go4expert.com/forums/data-structure-using-cpp-t27985/)

coolroose 9Mar2012 14:51

data structure using c++
 
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.

dearvivekkumar 9Mar2012 16:13

Re: data structure using c++
 
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

coolroose 21Mar2012 20:12

Re: data structure using c++
 
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,


All times are GMT +5.5. The time now is 22:07.