
Originally Posted by
Dmy
Thank you for the detailed answer!
I use IDE code :: blocks.
I do not know what #include is needed.
The truth is that the code does not compile.
Indeed, that's what I want, to compile an example.
And I'm happy to download and I'll learn how to use different libraries.
OK, well, it's worth mentioning that the specific page you posted is not trying to use STLport's slist; it is merely the name of the object.
Code:
list <string> :: iterator iter;
iter = find (slist.begin (), slist.end (), son);
slist.insert (iter, spouse);
Unfortunately, the author has neglected to declare slist in the quoted portion, but we can presume from the type of the iterator and how that iterator is used that slist is defined as a list<string> type variable.
Additionally, objects use the dot operator to call their member functions.
Code:
slist.insert (iter, spouse);
The stuff on the left side of the dot must be an object name, meaning that you are calling insert on something called slist.
Frankly, I did what you wouldn't do, and read the README and INSTALL files included with the STLport download. In order to install the library properly, you have to configure it with a specific compiler, and Microsoft compilers are the easiest to configure due to presets. Since you are using codeblocks, 1) you are using a port for windows of GCC and 2) GCC on windows happens to work horribly with STLport. It may be easier for you to learn with your compiler's version of the STL. Meaning:
use #include <list> instead of #include <stlport/list>
in fact don't include anything starting with stlport/
From there you can just read your book and practice. Since I'm so nice, I'll provide something to play with that should look a lot like your book.
Code:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main()
{
list<string> mylist;
list<string>::iterator loc;
string son = "Danny";
loc = std::find(mylist.begin(),mylist.end(),son);
if (loc == mylist.end())
{
cout << "So of course when you make a list, it's empty and searches with find() will fail.\n";
}
cout << "Let's add " << son << " to the list and repeat the search!\n";
mylist.push_back(son);
loc = find(mylist.begin(),mylist.end(),son);
if (loc != mylist.end())
{
cout << "Found it! " << *loc << "\n";
}
string spouse = "Bob";
mylist.insert(loc,spouse);
for (list<string>::iterator it = mylist.begin();
it != mylist.end();
++it)
{
cout << *it << "->";
}
std::cout << "\n";
cout << "Inserting more stuff...\n";
vector<string> svec(4);
svec[0] = "quasi";
svec[1] = "simba";
svec[2] = "frolo";
svec[3] = "scar";
mylist.insert(mylist.begin(),svec.begin(),svec.end());
svec.clear();
string el = "simba";
cout << "Let's erase the " << el << " element using what we\'ve learned.\n";
loc = find(mylist.begin(),mylist.end(),el);
mylist.erase(loc);
for (list<string>::iterator it = mylist.begin();
it != mylist.end();
++it)
{
cout << *it << "->";
}
cout << "\n";
}