码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Containers & algorithms

时间:2019-01-23 21:35:09      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:otherwise   call   nta   rar   hat   value   iat   ISE   cep   

Standard Template Library

Standard template library accepted in July 1994 into C++ ANSI Standard

STL library provides containers,iterators and algorithms designed to work effciently,work parametrically, work orthogonally

Sequence - ordered by sequence of elements

Associative - keys for looking up elements

 

Typical Container Interfaces

Constructors

default constructors, copy constructors

Element access, Element insertion,Element emplacement -new C++11(&&),Element deletion

Destructor,Iterators

 

Overview of Containers

Common set of properties, Constructors and destructors, Element access, insertion and deletion, Allocate and manage memory, Associated allocator objects

 

unordered_map

C++11 introduces "hash" based lookup for map and set

unordered_map - uses hash lookip  unordered_set - uses hash lookup

Advantage - much of the time O(1) lookup

#include<map>
#include<unordered_map>
#include<string>
#include<iostream>

using namespace std;
//we will use both types of map
int main(){
    map<unsigned long, string> worker;
    unordered_map<unsigned long, unsigned> payroll;
    unsigned total_pay=0;
    worker[99567800] = "Harold Fish";
    payroll[99567800] = 67300;
    worker[8567800] = "Phillip Fish";
    payroll[8567800] = 87300;
}

Worker links id number to name, Payroll links id number to salary

Ordinary map red-black tree, Unordered map hash

STL Algorithms Library

Sorting algorithms, Non-mutating sequence algorithms,Mutating sequence algorithms,Numerical algorithms,Generally use iterators to access containers instantiated on given type

Resulting code can be competitive in efficiency with special purpose codes

 

Sorting Algorithms:Prototypes

template<class RandAcc>
void sort(RandAcc b, RandAcc e);

Quicksort algorithm over elements b to e

template<class RandAcc>
void stable_sort(RandAcc b, RandAcc e);

Stable sorting algorithm over elements b to e

Elements remain in their relative same position

 

Non-mutating Sequence Algorithms

Do not modify contents of the containers they work on

Typical operation is searching container for particular element and returning its position

template<class Inputlter, Class T>
Inputlter find(Inputlter b, Inputlter e, const T& t);

Finds position of t in range  b to e

template<class Inputlter, Class Predicate>
Inputlter find_if(Inputlter b, Inputlter e, Predicate p);

Finds position of first element that makes predicate true in range b to e, otherwise position e returned

template<class Inputlter, Class Function>
void for_each(Inputlter b, Inputlter e, Function f);

Apply f to each value found in range b to e

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

int main(){  //find pos of "hop" 
    string words[5] = {"my", "hop", "mop", "hope", "cope"};
    string* where;
    where = find(words, words+5, "hop");
    cout << *++where << endl; //mop
    sort(words, words+5);
    where = find(words, words+5, "hop");
    cout << *++where << endl; //hope
}

 

C++ Containers & algorithms

标签:otherwise   call   nta   rar   hat   value   iat   ISE   cep   

原文地址:https://www.cnblogs.com/freeblacktea/p/10311195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!