Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 1.7: The C++ Standard Template Library

Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison- Wesley)

Program that demonstrates use of the C++ Standard Template Library

// Using the list container.
#include <iostream>
#include <list>
#include <numeric>
using namespace std;

void print(const list<double> &lst)
{ // using an iterator to traverse lst
    list<double>::const_iterator where;

    for(where = lst.begin(); where != lst.end(); ++where)
        cout << *where << '\t';
    cout << endl;
}

int main()
{
    double w[4] = {0.9, 0.8, 88, -99.99};
    list<double> z;

    for (int i = 0; i < 4; ++i)
        z.push_front(w[i]);
    print(z);
    z.sort();
    print(z);
    cout << "sum is " << accumulate(z.begin(), z.end(), 0.0) << endl;
}

The above program is compiled and run using Gnu Compiler Collection (g++):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/Chapter_1_7_The_Standard_Template_Library"
os.chdir(code_dir)
build_command = os.system("g++ stl_list.cpp -w -o stl_list")
exec_status = os.system("./stl_list")
-99.99	88	0.8	0.9	
-99.99	0.8	0.9	88	
sum is -10.29