Jump to content

C++: Difference between revisions

2,408 bytes added ,  8 January 2020
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
C++ is a very popular and powerful language which includes all the low-level features of [[C_(programming_language) | C]] (e.g. pointers, operator overloeading) along many high-level features (regex, STL containers) thanks to the C++ standard library.<br>
C++ is a very popular and powerful language which includes all the low-level features of [[C_(programming_language) | C]] (e.g. pointers, operator overloading) along many high-level features (regex, STL containers) thanks to the C++ standard library.<br>
Some people may think of it as an object-oriented version of C.
Some people may think of it as an object-oriented version of C.


Line 47: Line 47:
If you're casting between things but do not want to change the bit-pattern (e.g. binary data or pointers), you can also use <code>reinterpret_cast</code>.
If you're casting between things but do not want to change the bit-pattern (e.g. binary data or pointers), you can also use <code>reinterpret_cast</code>.


===Array===
===String===
<code>#include <array></code><br>
<code>#include <string></code><br>
In C++, you can use <code>std::vector</code> which gives you a resizable array.
 
This will allocate an array in the heap.<br>
<syntaxhighlight lang="cpp">
 
// c-str to string
char *old_string = "my c-style string";
string cpp_string(old_string);
 
// string to c-str
cpp_string.c_str();


[https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html array vs vector]<br>
// char to string
If you need a static sized array, you can use <code>std::array</code> in the <code>array</code> header.<br>
char my_char = 'a';
This wrapper around C-style arrays gives us size information and allows the array to be passed around by reference while keeping the array on the stack unlike <code>std::vector</code>.<br>
string my_str(1, my_char);
If you want to allocate a static array on the heap, you can do so as follows:
<syntaxhighlight lang="C++">
auto my_arr = std::make_shared<std::array<char,64>>();
</syntaxhighlight>
</syntaxhighlight>


===String===
<code>#include <string></code><br>
====String Interpolation====
====String Interpolation====
[https://stackoverflow.com/questions/10410023/string-format-alternative-in-c Reference]
[https://stackoverflow.com/questions/10410023/string-format-alternative-in-c Reference]
Line 79: Line 81:
}
}
</syntaxhighlight>
</syntaxhighlight>
====Buildings Strings====
[https://www.fluentcpp.com/2017/12/19/build-strings-from-plain-string-up-to-boost-karma/ The Complete Guide to Building Strings In C++]<br>
There are multiple ways of buildings strings in C++.<br>
Strings are mutable in C++.<br>
I typically use <code>+</code> or <code>ostringstream</code> to build strings.
===Filesystem===
===Filesystem===
<code>#include <fstream></code><br>
<code>#include <fstream></code><br>
Line 242: Line 251:
std::numeric_limits<float>::max();
std::numeric_limits<float>::max();
</syntaxhighlight>
</syntaxhighlight>
===Utility===
<code>#include <utility></code><br>
====std::move====
[https://en.cppreference.com/w/cpp/utility/move Ref]<br>
Use <code>std::move</code> to move containers.
===Algorithm===
====std::find====
[https://en.cppreference.com/w/cpp/algorithm/find Reference]<br>


==STL==
==STL==
Line 253: Line 272:


===Sequences===
===Sequences===
====std::array====
<code>#include <array></code><br>
In C++, you can use <code>std::vector</code> which gives you a resizable array.
This will allocate an array in the heap.<br>
[https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html array vs vector]<br>
If you need a static sized array, you can use <code>std::array</code> in the <code>array</code> header.<br>
This wrapper around C-style arrays gives us size information and allows the array to be passed around by reference while keeping the array on the stack unlike <code>std::vector</code>.<br>
If you want to allocate a static array on the heap, you can do so as follows:
<syntaxhighlight lang="C++">
auto my_arr = std::make_shared<std::array<char,64>>();
</syntaxhighlight>
====std::vector====
====std::vector====
[https://en.cppreference.com/w/cpp/container/vector Reference]<br>
Use vector for almost everything...<br>
It is an ArrayList.<br>
Note that <code>vector<bool></code> is not an array of bools.<br>
This has several nuances so you should use <code>vector<char></code> instead.<br>
<syntaxhighlight lang="c++">
// Basics
vector my_vec;
// Vector with size 5
vector my_vec(5);
// Vector with size 5 initialized to 1
vector my_vec(5, 1);
// Length of vector
my_vec.size();
// Equivalent to size()==0
my_vec.empty();
// Equivalent to my_vec[0];
// Undefined on empty vectors
my_vec.front();
// Equivalent to my_vec[my_vec.size()-1];
// Undefined on empty vectors
my_vec.back();
</syntaxhighlight>
====std::deque====
Double-ended queue
====std::list====
====std::list====


===Container adaptors===
===Container adaptors===
====std::queue====
====std::queue====
[https://en.cppreference.com/w/cpp/container/queue Reference]<br>
<syntaxhighlight lang="c++">
</syntaxhighlight>
====std::stack====
====std::stack====
[https://en.cppreference.com/w/cpp/container/stack cppreference]
<syntaxhighlight lang="cpp">
std::stack<char> my_stack;
// Push to stack
// You can also use emplace
// Returns void
my_stack.push('a');
// Peek
// Always make sure stack is not empty
char top = my_stack.top('a');
// Pop
// Note: returns void
// Always make sure stack is not empty
my_stack.pop();
</syntaxhighlight>


===Associative Containers===
===Associative Containers===
Line 273: Line 360:
</syntaxhighlight>
</syntaxhighlight>
====std::unordered_map====
====std::unordered_map====
;Custom Keys
How to use a rational number as a key in C++
<syntaxhighlight lang="C++">
struct Fraction
{
    int num;
    int den;
    bool operator==(const Fraction &other) const {
        return num*other.den == den * other.num;
    }
    Fraction(int a, int b) : num(a), den(b) {}
};
</syntaxhighlight>


==Boost==
==Boost==