Last active
August 16, 2019 00:59
-
-
Save rcanepa/229bc46957778629521f to your computer and use it in GitHub Desktop.
C++ Snippets
This file contains hidden or 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
/* | |
Short list of macros and templates from | |
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary | |
*/ | |
typedef vector<int> vi; | |
typedef vector<vi> vvi; | |
typedef pair<int,int> ii; | |
#define sz(a) int((a).size()) | |
#define pb push_back | |
#defile all(c) (c).begin(),(c).end() | |
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) | |
#define present(c,x) ((c).find(x) != (c).end()) | |
#define cpresent(c,x) (find(all(c),x) != (c).end()) | |
/* | |
Macro to set the beggining and end of a container. | |
*/ | |
#define all(c) c.begin(), c.end() | |
vector<int> v1 (100,0); | |
vector<int> v2 (all(v1)); | |
set<int> s (all(v1)); // Removes duplicates from v1 | |
/* | |
Macro to traverse a container using iterators | |
*/ | |
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++) | |
tr (container, it) | |
{ | |
cout << (*it) << endl; | |
} | |
/* | |
Macros to find elements in set/maps or vectors | |
*/ | |
// For sets and maps | |
#define present(container, element) (container.find(element) != container.end()) | |
// For vectors | |
#define cpresent(container, element) (find(all(container),element) != container.end()) | |
/* | |
Passing an iterator instead of a container. | |
This solution works even for arrays [] | |
*/ | |
template <class Iter> | |
double product( Iter start, Iter stop ) | |
{ | |
double prod = 1; | |
while ( start != stop ) prod *= *start++; | |
return prod; | |
} | |
Examples: | |
double nums[] = { 1.2, 3.0, 3.5, 2.8 }; | |
return product( nums, nums + 4 ); | |
std::vector<int> v = { 1.2, 3.0, 3.5, 2.8 }; | |
return product (v.begin(), v.end()); | |
/* | |
Tokenize a string 'in' with a delimiter 'kar'. | |
*/ | |
vector< string > tokenize( string in, string kar ) { | |
string::iterator cp = in.begin(); | |
vector< string > oot; | |
while( cp != in.end() ) { | |
while( cp != in.end() && count( kar.begin(), kar.end(), *cp ) ) | |
cp++; | |
if( cp != in.end() ) | |
oot.push_back( string( cp, find_first_of( cp, in.end(), kar.begin(), kar.end() ) ) ); | |
cp = find_first_of( cp, in.end(), kar.begin(), kar.end() ); | |
}; | |
return oot; | |
}; | |
/* | |
Another tokenize method working on a | |
string 'in' with delimiters inside 'token'. | |
*/ | |
#include <iostream> | |
#include <cstring> | |
#include <string> | |
#include <vector> | |
std::vector<std::string> tokenize_v2 (std::string in, const char * token) | |
{ | |
char * cstr = new char [in.length()+1]; | |
std::strcpy (cstr, in.c_str()); | |
std::vector<std::string> parsed_str; | |
// cstr now contains a c-string copy of in | |
char * p = std::strtok (cstr, token); | |
while (p!=0) | |
{ | |
parsed_str.push_back(p); | |
// std::cout << p << '\n'; | |
p = std::strtok(NULL, token); | |
} | |
delete[] cstr; | |
return parsed_str; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
std::string sentence = "I,am,a,string with blank spaces and commas"; | |
std::vector<std::string> splitted; | |
const char * delimiters = " ,.-"; | |
splitted = tokenize_v2(sentence, delimiters); | |
for (std::string x : splitted) | |
std::cout << x << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment