This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define COMMAND(NAME) { #NAME, NAME ## _command } | |
struct command commands[] = | |
{ | |
COMMAND (quit), | |
COMMAND (help), | |
... | |
}; | |
struct command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C++ concepts to learn: | |
SFINAE | |
pimpl | |
RTTI | |
C++ links to learn: | |
https://www.pluralsight.com/a/subscribe/step1?sku=IND-M-PLUS&isTrial=true&v=a | |
http://www.smu.edu/Guildhall/Admissions/GuildhallPreparation/ProgrammingPreparationCourse | |
http://www.codeguru.com/cpp/cpp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef assert | |
#undef assert | |
#endif | |
#define assert(expr,i) \ | |
if (!(expr)) { \ | |
std::cerr << "Error at line " << __LINE__ << ": " \ | |
<< "assert(" #expr ");" <<"value of i: "<<i<< std::endl; \ | |
exit(EXIT_FAILURE); \ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename FUNCTION> | |
inline void loop(int n, FUNCTION f) { | |
for (int i = 0; i < n; ++i) { | |
f(i); | |
} | |
} | |
// ... | |
loop(5, [](int jj) { std::cout << "This is iteration #" << jj << std::endl; } ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstddef> | |
using namespace std; | |
template <typename T> | |
class Array2D { | |
public: | |
Array2D(size_t nr, size_t nc) : n_rows(nr), n_cols(nc), data(new T[n_rows*n_cols]) {} | |
~Array2D() { delete [] data; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int compare (const void *elem1, const void *elem2){ | |
return *(int*)elem1 - *(int*)elem2; | |
} | |
#define ROW 5 | |
#define COL 2 | |
int main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # define NDEBUG | |
# include <assert.h> | |
class array2D{ | |
int rows,cols.**arr; | |
public: | |
// array2D():array2D(0,0,0){} | |
array2D(int r,int c,int val):rows(r),cols(c),arr(nullptr){ | |
arr = new int *[rows]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vector<char> toVector( const std::string& s ) { | |
string s = "apple"; | |
vector<char> v(s.size()+1); | |
memcpy( &v.front(), s.c_str(), s.size() + 1 ); | |
return v; | |
} | |
vector<char> v = toVector(std::string("apple")); | |
// what you were looking for (mutable) | |
char* c = v.data(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using next permutation | |
while(next_permutation(vec.begin(), vec.end()) ){ | |
// do some processing on vec | |
} | |
//using sort | |
std::sort (myvector.begin(), myvector.begin()+4) | |
std::sort (myvector.begin(), myvector.end()) | |
//intialise vector with another array |
NewerOlder