C++: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
=Usage= | |||
How to do things using the [https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library C++ standard library (stdlib)]. | |||
==Strings== | |||
===String Interpolation=== | |||
[https://stackoverflow.com/questions/10410023/string-format-alternative-in-c Reference] | |||
<syntaxhighlight lang="C++"> | |||
#include <iostream> | |||
#include <sstream> | |||
#include <string> | |||
== | int main() { | ||
std::string a = "a", b = "b", c = "c"; | |||
=== Reading and Writing === | // apply formatting | ||
std::stringstream s; | |||
s << a << " " << b << " > " << c; | |||
// assign to std::string | |||
std::string str = s.str(); | |||
std::cout << str << "\n"; | |||
} | |||
</syntaxhighlight> | |||
==Filesystem== | |||
===Reading and Writing=== | |||
Reading and writing is done using <code>fstream</code>.<br> | Reading and writing is done using <code>fstream</code>.<br> | ||
If you don't need r/w, use <code>istream</code> for reading or <code>ostream</code> for writing.<br> | If you don't need r/w, use <code>istream</code> for reading or <code>ostream</code> for writing.<br> | ||
Line 24: | Line 42: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Regular Expressions== | |||
[https://en.cppreference.com/w/cpp/regex Reference] | [https://en.cppreference.com/w/cpp/regex Reference] | ||
<!-- | <!-- | ||
Line 38: | Line 56: | ||
--> | --> | ||
==Threading== | |||
=== Sleep === | === Sleep === | ||
<syntaxhighlight lang="C++"> | <syntaxhighlight lang="C++"> | ||
Line 44: | Line 62: | ||
</syntaxhighlight > | </syntaxhighlight > | ||
==Memory== | |||
=== Garbage Collection === | === Garbage Collection === | ||
Traditional C++ does not have garbage collection. | Traditional C++ does not have garbage collection. | ||
Line 49: | Line 68: | ||
You can also use C allocation with `malloc`, `calloc`, `alloca`, and `free`. | You can also use C allocation with `malloc`, `calloc`, `alloca`, and `free`. | ||
If using C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection. | If using C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection. | ||
=Boost= | |||
=STL= |
Revision as of 12:38, 23 September 2019
Usage
How to do things using the C++ standard library (stdlib).
Strings
String Interpolation
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string a = "a", b = "b", c = "c";
// apply formatting
std::stringstream s;
s << a << " " << b << " > " << c;
// assign to std::string
std::string str = s.str();
std::cout << str << "\n";
}
Filesystem
Reading and Writing
Reading and writing is done using fstream
.
If you don't need r/w, use istream
for reading or ostream
for writing.
#include <iostream>
#include <fstream>
int main() {
std::istream my_file("my_file.txt");
std::string line;
# Read line by line
# You can also read using <<
while (getline(my_file, line)) {
std::cout << line << std::endl;
}
return 0;
}
Regular Expressions
Threading
Sleep
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Memory
Garbage Collection
Traditional C++ does not have garbage collection.
After using `new` to allocate an object, use `delete` to deallocate it.
You can also use C allocation with `malloc`, `calloc`, `alloca`, and `free`.
If using C++14, you can use shared pointers which does have automatic garbage collection.