Skip to content
Snippets Groups Projects
Commit 2471fc47 authored by Eric Elfving's avatar Eric Elfving
Browse files

stl intro

parents
No related branches found
No related tags found
No related merge requests found
# Föreläsningsexempel TDDI82 - 2018
Detta repo fylls på med exempelkod från föreläsningarna.
| Fil / Katalog | Beskrivning | Datum |
| ------------- | ----------- | ----- |
| STL-Intro | Visar ett litet exempel löst på flera olika sätt för att introducera standardbiblioteket | 2018-03-19 |
# Introduktion till standardbiblioteket
Ett litet exempel för att visa på de viktigaste delarna i standardbiblioteket. Samma problem löses på olika sätt. Vi har en uppsättning strängar som är strängrepresentationen av heltalsvärden. Målet är att skriva ut summan av heltalsvärdena.
| Fil | Beskrivning |
| --- | ----------- |
| ```stl.cc``` | Ingen direkt användning av STL (utöver vector). Visar en standardlösning på problemet. |
| ```stl_for-each.cc``` | Använder ```for_each``` med ett lambda-uttryck. Inte allt för vackert... |
| ```stl_transform.cc``` | Använder algoritmen ```transform``` för att stoppa in i en förallokerad vector. Visar även ```accumulate``` för summeringen. |
| ```stl_transform-backinserter.cc```| Bygger vidare på föregående och använder ```back_inserter``` (en ```back_insert_iterator```) för insättning. |
| ```stl_accumulate.cc``` | Tar bort mellansteget i att skapa en extra vector och använder ```accumulate``` tillsammans med ett lambda direkt på ursprungsvärdena.|
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int sum(vector<int> const & vals)
{
int result {};
for ( int i: vals )
{
result += i;
}
return result;
}
vector<int> to_ints(vector<string> const & v)
{
vector<int> result;
for ( string const & s: v )
{
result.push_back(stoi(s));
}
return result;
}
int main()
{
vector<string> values {"12", "11", "1", "2", "44"};
cout << sum(to_ints(values)) << endl;
}
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
vector<string> values {"12", "11", "1", "2", "44"};
// accumulate can take a function to use instead of normal
// operator +. The first argument is the total sum until the
// current element and the second is the current element.
// The return value should be the new sum.
cout << accumulate(begin(values), end(values), 0,
[](int v, string const & s)
{
return v + stoi(s);
});
cout << endl;
}
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int sum(vector<int> const & vals)
{
int result {};
for ( int i: vals )
{
result += i;
}
return result;
}
vector<int> to_ints(vector<string> const & v)
{
vector<int> result;
// for_each accepts an open range of values (given by iterators)
// and a function to apply to each element in that range.
// Here we provide a lambda (an anonymous "function" that binds
// the vector result by reference to be able to modify it. By
// default (if the []-part "capture block" is left empty), the
// lambda will only have acces to it's parameters.
for_each(begin(v), end(v),
[&result](string const & s){
result.push_back(stoi(s));
});
return result;
}
int main()
{
vector<string> values {"12", "11", "1", "2", "44"};
cout << sum(to_ints(values)) << endl;
}
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int sum(vector<int> const & vals)
{
return accumulate(begin(vals), end(vals), 0);
}
vector<int> to_ints(vector<string> const & v)
{
vector<int> result;
// transform takes a range of values (input range) and an
// output range specified with it's first position. The
// given function is called for each element in the input
// range and the result of each call is put into the output
// range.
// back_inserter creates a special iterator that calls push_back
// on the provided container when we try to write to it
transform(begin(v), end(v), back_inserter(result),
[](string const & s){return stoi(s);});
return result;
}
int main()
{
vector<string> values {"12", "11", "1", "2", "44"};
cout << sum(to_ints(values)) << endl;
}
#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int sum(vector<int> const & vals)
{
return accumulate(begin(vals), end(vals), 0);
}
vector<int> to_ints(vector<string> const & v)
{
vector<int> result;
result.resize(v.size());
// transform takes a range of values (input range) and an
// output range specified with it's first position. The
// given function is called for each element in the input
// range and the result of each call is put into the output
// range.
// transform (or any other algorithm) will not (and can not)
// modify the size of the container. The output range has to
// be at least as large as the input range.
transform(begin(v), end(v), begin(result),
[](string const & s){return stoi(s);});
return result;
}
int main()
{
vector<string> values {"12", "11", "1", "2", "44"};
cout << sum(to_ints(values)) << endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment