C++ notes

Collection of random notes and snippets I’ve needed to look up too often.

These are not in any particular order or arranged anyhow.

Most of these are joinked from Stackoverflow.



For loop

for (n = 0; n < 16; n++)
{
}

See if string contains a substring

std::string longstring = "BMW_is_a_german_car_brand";
std::string substring = "BMW";

if (longstring.find(substring) != std::string::npos) {
    std::cout << "found!" << '\n';
}

Read a file line by line

#include <fstream>

std::ifstream input( "filename.ext" );
for( std::string line; getline( input, line ); )
{
    ...for each line in input...
}