STL container?

Pages: 12
can someone help me with what STL is? I mean i have a program but i am not sure on how to implement it using STL containers like which type of STL container and algorithm it uses?
It is a shorthand for Standard Template Library. Some time ago, in dark ages, when there weren't so many standard containers, algorithms and stuff, some guy had created a template library to work with anything. It was so popular, that it was included in standard later. Now STL doesn't exist as a separate entity. Some people calls templated part of standard library STL, but it is not correct.

Seriously. It is on first page of google: http://lmgtfy.com/?q=STL+wikipedia&l=1
That LMGTFY link opens this for me:
http://en.wikipedia.org/wiki/STL_%28file_format%29

Google spies on us... and customizes its searches depending on what it thinks we're interested in... I don't like Google very much.
Wikipedia has a search feature as well. If you don't like Google make sure to block googlesyndication.com and google-analytics.com that runs on this site.
Thank you all for replying.. I understood the concept of STL the only thing that is not clear to me is in a normal program how can i add STL container?
I mean i have a program which is without STL now need to add one STL container don know how to do an someone guide me with it please..
how can i add STL container?
std::vector is an STL container. std::array, std::list, std::map, std::deque, std::set, std::forward_list, std::unordered_map, std::unordered_set, all of these are STL containers.

I mean i have a program which is without STL
STL is a part of standard library now. I bet you already have part of something which were in STL before in your program already.
The STL includes several container classes, with different properties. Your first step should be to read the basic descriptions of them, and decide which one is most suitable for whatever it is that you want to use it for.

There's plenty of documentation about them on this site.
@MiiNiPaa
If std::map is a STL containter, what do you call std::unordered_map? A boost container?
hash_map was a part of original STL. It is that standard comitee doesn't adopt it until C++11.
std::unordered_map is an STL container, as of the C++11 standard.

Prior to that, it wasn't part of the STL. However, boost::unordered_map was part of the Boost libraries.
hash_map was a part of original STL. It is that standard comitee doesn't adopt it until C++11.
As I understand it, hash_map was an extension to the STL provided by many C++ implementations, but wasn't formally part of the standard until the unordered_map class was introduced as part of TR1 and then C++11.

Edit: Actually, it's more correct to say that hash_map wasn't part of the Standard C++ Library. The STL documentation at http://www.sgi.com/tech/stl/ includes hash_map.
Last edited on
STL was external library, that was later incorporated in standard C++. AFAIR HP implementation was first. STL does not exist officially in C++ standard. This is just a name some people calls parts that were influenced by original STL.
Ah, OK. This thread actually prompted me to look it up, and I've just gone back and edited my post. Thanks for the clarification :)
There were quite a few things in STL that weren't included in C++. I loved the compose and select functors, power() (which I still have to write by hand every time) and iota() which made a comeback for C++11.

Many general-purpose C++ libraries back then (1992-1998) had containers (including hash maps of course), iterators, and some sort of algorithms, but STL had the best design and the smallest scope (no threads, strings, I/O, or date/time like the others)

But this thread really ran off course. OPs question was "i have a program but i am not sure on how to implement it using containers and algorithms", and it's impossible to answer without the program in question, or at least a detailed description of what it does.
@MiiNiPaa and MickeyBoy-- thank you for giving me the concept of the STL it vil surely help me..

@Cubbi-- Actually you are right i need to what the program actualy is and how STL is impelemented..
Here is the code for counting the occurrence of words and please do help me in knowing how can i add STL container and algorithm in it...



#include <iostream>
#include <string>


using namespace std;

void count_occurances(const string the_line);

int main(){
char hold_console;

string line;
cout<<"Enter line\n";
cout<<">>";
getline(cin,line);

count_occurances(line);

cin>>hold_console;

return 0;
}
void count_occurances(const string the_line){

int a_count=0,b_count=0, c_count=0, d_count=0;

for(int i=0;i<the_line.length();i++){

if(isalpha(the_line[i])){

if(the_line[i]=='a' || the_line[i]=='A')a_count++;
else if(the_line[i]=='b' || the_line[i]=='B')b_count++;
.......

}



}
cout<<"a or A occured: "<<a_count<<" times\n";
.......


}
That can be done with just one C++ container, map:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <cctype>
#include <map>

using namespace std;

void count_occurances(const string& the_line)
{
    map<char, unsigned> counts;

    for(char c: the_line)
        if(isalpha(c))
            ++counts[toupper(c)];

    for(const auto& p: counts)
        cout << char(tolower(p.first)) << " or " << p.first << " occurred: "
             << p.second << " times\n";
}

int main()
{
    cout << "Enter line\n"
            ">>";
    string line;
    getline(cin, line);

    count_occurances(line);
}

online demo: http://ideone.com/dM7UXb

you could use the algorithm for_each instead of either of the two loops if you like
Last edited on
@Cubbi-- thank u so much for the help.
But i am unable to debug the program with some errors;
Could u please help me with it.
I am running the program using visual studio 2010...

error C2143: syntax error : missing ',' before ':'
.cpp(16): error C2143: syntax error : missing ',' before ':'
.cpp(16): error C2530: 'p' : references must be initialized
.cpp(16): error C3531: 'p': a symbol whose type contains 'auto' must have an initializer
.cpp(17): error C2228: left of '.first' must have class/struct/union
1> type is 'int'
.cpp(17): error C2228: left of '.first' must have class/struct/union
1> type is 'int'
.cpp(18): error C2228: left of '.second' must have class/struct/union
1> type is 'int'
1>
1>Build FAILED.
Cubbi's using C++11 constructs to iterate over the map.

If your compiler doesn't support C++, then read up on iterators, so that you can use them to do the same thing
@mickeyboy- it does support c++, i run the program with no STL container and it produced output.. but i need that program with the use of STL...

Sorry, that should have read "If your compiler doesn't support C++11..."
Pages: 12