Breaking a complex problem or system into a collection of smaller parts
When performed correctly, the smaller parts are easier to:
Breaking a complex algorithm into a collection of smaller algorithms
Breaking a function down into a collection of smaller functions
Hide the internal details of a part, algorithm, or function, and only expose the minimal interface
Direction | Example | Type |
---|---|---|
IN | void f(int data); |
value |
void f(const Data& data); |
const reference | |
void f(const Data* data); |
const pointer | |
OUT | void f(int& data); |
reference |
void f(Data& data); |
reference | |
void f(Data* data); |
pointer | |
void f(Data** data); |
pointer to pointer | |
int f(); |
return | |
Data f(); |
return | |
const Data& f(); |
return | |
IN/OUT | void f(int& data); |
reference |
void f(Data& data); |
reference | |
void f(Data* data); |
pointer | |
void f(Data** data); |
pointer to pointer |
Type | Example | Direction |
---|---|---|
value | void f(int data); |
IN |
const reference | void f(const Data& data); |
IN |
reference | void f(int& data); |
OUT |
void f(Data& data); |
OUT | |
void f(int& data); |
IN/OUT | |
void f(Data& data); |
IN/OUT | |
const pointer | void f(const Data* data); |
IN |
pointer | void f(Data* data); |
OUT |
void f(Data* data); |
IN/OUT | |
pointer to pointer | void f(Data** data); |
OUT |
void f(Data** data); |
IN/OUT | |
return | int f(); |
OUT |
return | Data f(); |
OUT |
return | const Data& f(); |
OUT |
#include <string> | |
#include <vector> | |
/* | |
Split a CSV line into a container of strings | |
@param[in] str The CSV string to split | |
@return The container of parts of the string | |
*/ | |
std::vector<std::string> splitCSV(const std::string& line) { | |
const char SPLIT_TOKEN = ','; | |
// extract all fields | |
std::vector<std::string> fields; | |
std::size_t position = 0; | |
while (position < line.size()) { | |
// find the postion of the split token | |
const auto splitPosition = line.find(SPLIT_TOKEN, position); | |
// when the split token does not exist, then the rest of the | |
// string is the last element | |
if (splitPosition == std::string::npos) { | |
fields.push_back(line.substr(position)); | |
break; | |
} | |
// the current field is from the last position to this one | |
fields.push_back(line.substr(position, splitPosition - position)); | |
// move after the current position | |
position = splitPosition + 1; | |
} | |
return fields; | |
} |