stl

Appropriate container for the fast insertion and lookup of n-dimensional real vectors (initial benchmarking provided)

家住魔仙堡 提交于 2019-12-22 09:49:13
问题 1. Description of the problem I am trying to pick the most appropriate (efficient) container to store unique n -dimensional vectors composed of floating numbers. Solving whole problem, the most important steps (related to the question) involve: Get a new vector from the external program (during the same run, all vectors have the same dimensionality). Check (ASAP) if a new point is already in this container: if exists - skip a lot of expensive steps and do the other steps; if doesn't - insert

How to store templated objects in an STL container and member function call

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:45:42
问题 Suppose you have a class like template<class T> struct A { void foo() { // Need access to "T" here typedef typename someTrait<T>::someType T2; } }; and you would like to "register" (or store) instances of the class (or a pointers to it) with a container (probably STL) for later calling the foo() method of all registered instances. Since instances instantiated with different template parameters are to be stored ( A<int> , A<float> , ...) obviously one can't use a std::vector and store the

Now to remove elements that match a predicate?

强颜欢笑 提交于 2019-12-22 09:42:26
问题 I have a source container of strings I want to remove any strings from the source container that match a predicate and add them into the destination container. remove_copy_if and other algorithms can only reorder the elements in the container, and therefore have to be followed up by the erase member function. My book (Josuttis) says that remove_copy_if returns an iterator after the last position in the destination container. Therefore if I only have an iterator into the destination container,

Bind const std::pair<T, U>& to value of std::pair<const T, U>

北城以北 提交于 2019-12-22 09:41:40
问题 The below code snippet compiles with a very important warning. #include <map> #include <vector> template <typename iterator> const std::pair<int, float> &foo(iterator it) { return *it; } int main() { std::vector<std::pair<int, float>> vector; std::map<int, float> map; vector.push_back(std::make_pair(0, 0.0)); map.insert(std::make_pair(0, 0.0)); const std::pair<int, float> &r1 = foo(vector.begin()); const std::pair<int, float> &r2 = foo(map.begin()); if (r1 != r2) { return 1; } return 0; }

number of matches in two sequences with STL

梦想与她 提交于 2019-12-22 09:40:02
问题 OK, here is yet another question of the "How to do it better in STL?" series. We have two ranges, designated by first1, last1, and first2. We want to find the number of different i's from [0, last1-first1] such that *(first1 + i) == *(first2 + i) For example: {a, b, c, d, d, b, c, a} {a, a, b, c, d, c, c, a} ^ ^ ^ ^ For these two ranges the answer is 4. Is there a good STL way to do it? I mean preferrably without any manual for's, while's etc. Thanks! 回答1: std::inner_product(first1, last1,

How do I define a “unary predicate” for copy_if, etc in C++?

扶醉桌前 提交于 2019-12-22 09:39:58
问题 I'm trying to use std::copy_if() and I figured out somewhat how the syntax works from http://www.cplusplus.com/reference/algorithm/copy_if/ : auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} ); The last argument is the one that I am confused about. What are the brackets for? Can I use a function I wrote somewhere else as an argument and how would that work? Can I pass another argument to the function if I specify which variable to pass to it? I guess my

g++ 4.8.1 on Ubuntu can't compile large bitsets

会有一股神秘感。 提交于 2019-12-22 09:39:52
问题 My source: #include <iostream> #include <bitset> using std::cout; using std::endl; typedef unsigned long long U64; const U64 MAX = 8000000000L; struct Bitmap { void insert(U64 N) {this->s.set(N % MAX);} bool find(U64 N) const {return this->s.test(N % MAX);} private: std::bitset<MAX> s; }; int main() { cout << "Bitmap size: " << sizeof(Bitmap) << endl; Bitmap* s = new Bitmap(); // ... } Compilation command and its output: g++ -g -std=c++11 -O4 tc002.cpp -o latest g++: internal compiler error:

STL containers speed vs. arrays

北城以北 提交于 2019-12-22 09:36:21
问题 I just started working on a scientific project where speed really matters (HPC). I'm currently designing the data structes. The core of the project is a 3D-Grid of double values, in order to solve a partial differenital equation. Since speed here is a probably bigger concern then simplicity of the code, I'd like to know how the STL performs compared to usual C-style arrays. In my case, since it's a 3D-grid, I was thinking of a) a one dimensional vector with linear indexing b) a vector of 3

How can I sort a std::list with case sensitive elements?

余生颓废 提交于 2019-12-22 09:25:41
问题 This is my current code: #include <list> #include <string> using std::string; using std::list; int main() { list <string> list_; list_.push_back("C"); list_.push_back("a"); list_.push_back("b"); list_.sort(); } Does the sort() function sort the elements according to their character codes? I want the result here to be a b C after the sorting is done. 回答1: The default comparator ( < ) using the default char_traits< char > will sort your list as C a b . See list::sort. In order to achieve the

How to get data out of the STL's const_iterator?

被刻印的时光 ゝ 提交于 2019-12-22 08:59:33
问题 I have something that runs like this: T baseline; list<T>::const_iterator it = mylist.begin(); while (it != mylist.end()) { if (it == baseline) /* <----- This is what I want to make happen */ // do stuff } My problem is that I have no idea how to extract the data from the iterator. I feel like this is a stupid thing to be confused about, but I have no idea how to do it. EDIT : Fixed begin.end() 回答1: Iterators have an interface that "looks" like a pointer (but they are not necessarily pointers