std

Passing a private method of the class as the compare operator for std::sort

强颜欢笑 提交于 2019-12-05 16:19:43
I am writing a code to solve the following problem: Given a set of numbers x[0] , x[1] , ..., x[N-1] , find the permutation that makes them sorted in the ascending order. In the other words, I would like to find a permutation on {0,2,...,N-1} such as i[0] , i[1] , ..., i[N-1] such that x[i[0]] <= x[i[1]] <= ... <= x[i[N-1]] . For this, I have stored the x vector and an index vector i (initially filled with i[j] = j ) as private members of a class. I have also defined a private method as bool MyClass::compare(size_t s, size_t t) { return (x[s] < x[t]); } Now, I would call the std::sort as

How to sort two arrays/vectors in respect to values in one of the arrays, using CUDA/Thrust

ε祈祈猫儿з 提交于 2019-12-05 16:16:00
This is a conceptual question in regards programming. To summarize, I have two arrays/vectors and I need to sort one with the changes propagating in the other as well, so that if I sort arrayOne, for each swap in the sort - the same thing happens to arrayTwo. Now, I know that std::sort allows you to define a comparison function (for custom objects I assume) and I was thinking of defining one to swap arrayTwo at the same time. So what I want is - to sort the two vectors based on values in one of the vectors, using CUDA. This is where my uncertainty rises, essentially I want to use the Thrust

boost::filesystem::path and fopen()

陌路散爱 提交于 2019-12-05 16:05:32
问题 I get error when I try to do this: path p = "somepath"; FILE* file = fopen(p.c_str(), "r"); I get: argument of type "const boost::filesystem::path::value_type *" is incompatible with parameter of type "const char *" Could anyone tell me what I'm doing wrong? Thanks 回答1: If you're under Windows, that value_type is wchar_t , and will fail in the conversion for fopen (that needs a char* ). As per the documentation, it seems you have to use the string() method to obtain a standard string with a

How I can define a list of map::iterator and map of list::iterator

断了今生、忘了曾经 提交于 2019-12-05 16:02:05
I need a list of Map::iterator and map of List::iterator. How I can do this: typedef std::list<Map::iterator> List; typedef std::map<int, List::iterator> Map; Maybe I can use something like forward declaration for iterator? Something like this should help you: #include <cassert> #include <iostream> #include <list> #include <map> #include <string> struct decl_t { typedef std::map<std::string, decl_t> map_t; typedef std::list<std::pair<int, typename map_t::iterator>> list_t; list_t::iterator it; }; int main(int argc, const char* argv[]) { decl_t::map_t map; decl_t::list_t list; auto list_it =

How to convert std::vector<unsigned char> to vector<char> without copying?

醉酒当歌 提交于 2019-12-05 11:52:50
问题 I weren't able to find that question, and it's an actual problem I'm facing. I have a file loading utility that returns std::vector<unsigned char> containing whole file contents. However, the processing function requires contiguos array of char (and that cannot be changed - it's a library function). Since the class that's using the processing function stores a copy of the data anyway, I want to store it as vector<char> . Here's the code that might be a bit more illustrative. std::vector

How to derive from C++ std::basic_ostream and make the << operator virtual?

北战南征 提交于 2019-12-05 10:16:24
I am writing a class that has various messages output. I want to make this class general and platform independent, so I am thinking of passing a basic_ostream reference to it and it can dump all the messages into the stream. By doing this, if the class is used in a console program, I can pass std::cout to it and display in console window. Or I could pass a derived ostream to it and redirect the message to some UI components, e.g. ListBox? The only problem is the data inserter operator << is not a virtual function. If I pass the derived class reference to it, only the basic_ostream << operator

Is there a way to optimize std algorithms?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 10:14:29
Searching any information about std algorithms performance, I've found the Stack Overflow question about performance difference between std::max_element() and self-written function. I've tested the functions from the question with GCC 9.2.0 and found no performance difference, i.e. my_max_element_orig() and my_max_element_changed() (from the accepted answer) show the same performance. So it seems like it was just an optimizer issue in GCC 4.8.2. What I really found for GCC 9.2.0 is the significant difference in case I use pointers and iterators - using iterators is about 2 times worse against

std::any across shared library bounding in mingw

别等时光非礼了梦想. 提交于 2019-12-05 09:57:33
I stumbled about an issue while using libstdc++'s std::any implementation with mingw across a shared library boundary. It produces a std::bad_any_cast where it obviously should not (i believe). I use mingw-w64, gcc-7 and compile the code with -std=c++1z. The simplified code: main.cpp: #include <any> #include <string> // prototype from lib.cpp void do_stuff_with_any(const std::any& obj); int main() { do_stuff_with_any(std::string{"Hello World"}); } lib.cpp: Will be compiled into a shared library and linked with the executable from main.cpp. #include <any> #include <iostream> void do_stuff_with

iterate through two std::lists simultaneously

戏子无情 提交于 2019-12-05 08:01:00
Sorry if this is too simple a question. Prior error checking ensures l1.size() == l2.size() . std::list<object1>::iterator it1 = l1.begin(); std::list<object2>::iterator it2 = l2.begin(); while(it1 != l1.end() && it2 != l2.end()){ //run some code it1++; it2++; } Is this a reasonable approach, or is there a more elegant solution? Thanks for your help. I prefer to use for if increments unconditionally occurs: for(; it1 != l1.end() && it2 != l2.end(); ++it1, ++it2) { //run some code } You can omit one test while the size of lists are the same, but I'm not sure what's going on in run some code ! I

'powf' is not a member of 'std'

北慕城南 提交于 2019-12-05 07:50:48
Hi i have this error on a library that i have to compile for XCode. The <cmath> is included. Can someone explain to me what is going wrong ? Thanks. Up until C++11, powf was just a Microsoft-ism . It did not appear in the ISO standard at all so is unlikely to be in XCode unless they were to adapt Microsoft's bizarre practices, something I would think unlikely. pow , on the other hand, has been part of the C++ library for longer by virtue of the fact that it's in earlier iterations of the C library that is incorporated into C++ pre-11. Use that instead. Since C++11, powf does appear in the ISO