C++: Difference between revisions

From David's Wiki
No edit summary
No edit summary
Line 23: Line 23:
}
}
</syntaxhighlight>
</syntaxhighlight>
===Regular Expressions===
[https://en.cppreference.com/w/cpp/regex Reference]
<!--
<syntaxhighlight lang="C++>
#include <iostream>
#include <regex>
int main() {
  std::regex myRegex("(\\d+)");
  return 0;
}
</syntaxhighlight>
-->





Revision as of 20:31, 18 September 2019



Standard Library

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

Reference


Sleep

std::this_thread::sleep_for(std::chrono::milliseconds(1));

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.