data structure using c++

coolroose's Avatar, Join Date: Nov 2011
Newbie Member
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.
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
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
0
coolroose's Avatar, Join Date: Nov 2011
Newbie Member
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,