Skip to content

Instantly share code, notes, and snippets.

@slwu89
Last active July 21, 2017 16:22
Show Gist options
  • Save slwu89/e7cd7fb2c374d73b15d2db20f87652d5 to your computer and use it in GitHub Desktop.
Save slwu89/e7cd7fb2c374d73b15d2db20f87652d5 to your computer and use it in GitHub Desktop.
C++ tricks
#include <algorithm>
#include <iostream>
#include <vector>
// quickly find the position of the minimum and maximum elements of an STL vector
int main()
{
std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 };
auto result = std::minmax_element(v.begin(), v.end());
std::cout << "min element at: " << (result.first - v.begin()) << '\n';
std::cout << "max element at: " << (result.second - v.begin()) << '\n';
}
// find the min and max seperately, but getting slightly more information.
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<double> v {1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0};
auto biggest = std::max_element(std::begin(v), std::end(v));
std::cout << "Max element is " << *biggest
<< " at position " << std::distance(std::begin(v), biggest) << std::endl;
auto smallest = std::min_element(std::begin(v), std::end(v));
std::cout << "min element is " << *smallest
<< " at position " << std::distance(std::begin(v), smallest) << std::endl;
}
// fake '...' argument in Rcpp
void testEllipses(Function fun, double x, List ellipses){
double out = as<double>(fun(x,ellipses));
Rcout << out << std::endl;
}
// take one thing out of a vector quickly
int main()
{
std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 };
for (auto i = v.begin(); i != v.end(); ++i){
std::cout << *i << ' ';
}
std::cout << "" << std::endl;
v[1] = v.back(); // remove 2nd element from vector
v.pop_back();
for (auto i = v.begin(); i != v.end(); ++i){
std::cout << *i << ' ';
}
}
// find stuff
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<double> v;
v.push_back(1);
v.push_back(1);
v.push_back(10);
v.push_back(1);
v.push_back(1);
v.push_back(10);
v.push_back(1);
v.push_back(7);
v.push_back(1);
std::vector<int> results;
auto it = std::find_if(std::begin(v), std::end(v), [](int i){return i > 5;});
while (it != std::end(v)) {
results.emplace_back(std::distance(std::begin(v), it));
it = std::find_if(std::next(it), std::end(v), [](int i){return i > 5;});
for(auto it = results.begin(); it != results.end(); it++){
std::cout << "it: " << *it << std::endl;
}
}
}
// testing subsets of vectors
std::vector<std::string> stringVector;
stringVector.push_back("A");
stringVector.push_back("B");
stringVector.push_back("C");
stringVector.push_back("D");
stringVector.push_back("E");
for(auto it = stringVector.begin(); it != stringVector.end(); it++){
std::cout << "it: " << *it << std::endl;
}
std::vector<int> SelectionV;
SelectionV.push_back(0);
SelectionV.push_back(2);
for(auto it = SelectionV.begin(); it != SelectionV.end(); it++){
std::cout << "it: " << *it << std::endl;
}
std::vector<std::string> Result;
Result.reserve(SelectionV.size()+10);
std::transform(SelectionV.begin(), SelectionV.end(), std::back_inserter(Result), [stringVector](size_t pos) {
return(stringVector.at(pos));
});
std::cout << "start!" << Result.size() << std::endl;
for(auto it = Result.begin(); it != Result.end(); it++){
std::cout << "it: " << *it << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment