Coder Social home page Coder Social logo

42_cpp_08's Introduction

42_cpp_08

My solution for 42 Berlin's cpp_08 project.

Study notes

Templated Containers

In C++, templated containers are a part of the Standard Template Library (STL). They are generic types that allow you to store and manipulate collections of items. The type of items can be specified at the time of creating the container. Some of the most commonly used templated containers include std::vector, std::list, std::map, etc.

For example, to create a vector of integers, you would do:

std::vector<int> myVector;

Type of containers in C++

Sequence Containers

Class templates that implement data structures that can be accessed sequentially.

  • vector
  • deque
  • list
  • array (C++11)
  • forward_list (C++11)

Associative Containers

Class templates that implement sorted data structures that can be quickly searched with the use of keys.

  • set collection of unique keys, sorted by keys
  • map collection of key-value pairs, sorted by unique keys
  • multiset collection of keys, sorted by keys
  • multimap collection of key-value pairs, sorted by keys

Unordered Associative Containers (C++11)

Class templates that implement unsorted (hashed) data structures that can be quickly searched on average (O(1)), worst case O(n).

  • unordered_set collection of unique keys, hashed by keys
  • unordered_map collection of key-value pairs, hashed by unique keys
  • unordered_multiset collection of keys, hashed by keys
  • unordered_multimap collection of key-value pairs, hashed by keys

Iterators

Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a way to access the elements of a container (like arrays or lists) sequentially without exposing the underlying details of the container's implementation.

Types of Iterators

There are five types of iterators in C++:

  1. Input Iterators: They are used to read sequential data from a container.
  2. Output Iterators: They are used to write sequential data to a container.
  3. Forward Iterators: They are a combination of input and output iterators and can move in a forward direction.
  4. Bidirectional Iterators: They are like forward iterators, but can also move in a backward direction.
  5. Random Access Iterators: They can move directly to any element in the container, not just the next or previous one.

Using Iterators

Iterators are used with a specific syntax that is similar to pointers. Here is an example of how to use an iterator with a std::vector:

std::vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);

// Create an iterator for the vector
std::vector<int>::iterator it;

// Use the iterator to loop through the vector
for(it = myVector.begin(); it != myVector.end(); ++it) {
    std::cout << *it << std::endl;
}

Algorithms

The STL also provides several generic algorithms that can be used with its containers. These algorithms include common operations such as sorting (std::sort), searching (std::find), and more.

For example, to sort a vector of integers, you would do:

std::vector<int> myVector;
myVector.push_back(5);
myVector.push_back(3);
myVector.push_back(1);
myVector.push_back(4);
myVector.push_back(2);
std::sort(myVector.begin(), myVector.end());

To find an element in the vector, you would do:

std::vector<int>::iterator it = std::find(myVector.begin(), myVector.end(), 3);
if (it != myVector.end()) {
    std::cout << "Found: " << *it << std::endl;
} else {
    std::cout << "Not found :(" << std::endl;
}

42_cpp_08's People

Contributors

migmanu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.