std

Sort vector by even and odd indices. c++

限于喜欢 提交于 2019-12-12 08:47:12
问题 Is there a one liner (or a simple loop-free) solution to sort a vector by its even and odd indices? Example: long entries[] = {0,1,2,10,11}; // indices 0 1 2 3 4 std::vector<long> vExample(entries, entries + sizeof(entries) / sizeof(long) ); vExample.sortEvenOdd(vExample.begin(),vExample.end()); // magic one liner I wish existed... for (int i = 0; i < vExample.size(); i++) { std::cout << vExample[i] << " "; } Now I'd like to have the following output: 0 2 11 1 10 // corresponding to indices 0

When to use std::string vs char*? [duplicate]

試著忘記壹切 提交于 2019-12-12 08:06:21
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++ char* vs std::string I'm new to C++ coming from C# but I really do like C++ much better. I have an abstract class that defines two constant strings (not static). And I wondered if a const char* would be a better choice. I'm still getting the hang of the C++ standards, but I just figured that there really isn't any reason why I would need to use std::string in this particular case (no appending or editing the

Use the C++ STL in Enterprise Architect

痞子三分冷 提交于 2019-12-12 08:01:50
问题 How is it possible to use parts of the C++ STL in Enterprise Architect? It would be nice to be able to specify certain class attributes as std::string or use std::auto_ptr (or even std::tr1::shared_ptr ) as types. Another interesting thing would be how one is able to integrate container-types like std::vector and std::map into EA. 回答1: I have taught how STL containers look like to EA, I guess it can be extended to stl pointers too: Forward engineering: You can define collection classes for

map vs unordered_map for few elements

做~自己de王妃 提交于 2019-12-12 07:47:14
问题 I am trying to choose between map and unordered_map for the following use case: The key of the map is a pointer. The most common use case is that there will be a single element in the map. In general, the max number of elements in the map less than 10. The map is accessed very often and speed is the most important factor. Changes to the map are infrequent. While measuring the speed is obviously the correct approach here, this code will be used on several platforms so I'm trying to create a

Initialise size of std::array in a constructor of the class that uses it

半腔热情 提交于 2019-12-12 07:27:35
问题 Is it possible to use the std::array<class T, std::size_t N> as a private attribute of a class but initialize its size in the constructor of the class? class Router{ std::array<Port,???> ports; //I dont know how much ports do will this have public: Switch(int numberOfPortsOnRouter){ ports=std::array<Port,numberOfPortsOnRouter> ports; //now I know it has "numberOfPortsOnRouter" ports, but howto tell the "ports" variable? } } I might use a pointer, but could this be done without it? 回答1: No,

fill structure while finding min_element, C++

别来无恙 提交于 2019-12-12 06:45:47
问题 I want to fill some structure while finding minimum element. Pl find the code below tyoedef struct Point { double x, y; }Point; I have a vector of points - std::vector<Point> V in which i have few thousand points. There is another struct I have typedef struct cart { Point pt; double val_1; // computed using only Pt double val_2; // computer using only Pt }cart; Now I have two tasks: I need to find minimum element from structure V. Fill the structure cart, which is directly dependent on V. I

C++ map - expression must be an integral constant expression [duplicate]

被刻印的时光 ゝ 提交于 2019-12-12 06:07:18
问题 This question already has answers here : Am I missing anything here in my statement about c++? (5 answers) Closed 4 years ago . #include <map> #include <string> std::map<std::string, int> foo; foo["bar"] = 1; Why do I get the error "expression must be an integral constant expression" in visual studio 12? I can't work this one out... 回答1: You need to place the code inside a function. #include <map> #include <string> void xyz() { std::map<std::string, int> foo; foo["bar"] = 1; } I verified VS

stdlib-like library on bare metal environment? (memory management and hopefully pthread support)

雨燕双飞 提交于 2019-12-12 05:26:41
问题 Is there any stdlib-like library for bare-metal programming? I am trying to build a program (supposed to be built on linux) for bare-metal environment. The program is dependent on stdlib and posix lib (malloc, calloc, realloc, free, and pthread usage). I will modify it for single thread anyway. I was reading https://www.ibm.com/developerworks/aix/tutorials/au-memorymanager/ and maybe I will implement my own memory management. But in my case the program has malloc/realloc/free s of various

C++ - Developing own version of std::count_if

若如初见. 提交于 2019-12-12 04:39:25
问题 For a task, I'm doing some simple data sampling to determine which samples contains audio counting the total number of energy. I've been looking into the std::count_if function, and, although this suits my needs to a certain extent, for example: int foo = std::count_if( std::begin(samples), std::end(samples), containsSound); This counts the total number of samples that contain sound, but does not give an indication to the samples that contain sound. I came up with this solution: std::vector

How to convert CMSampleBuffer to std::vector<char>?

半腔热情 提交于 2019-12-12 03:33:41
问题 I have image stream from my AVCaptureVideoDataOutputSampleBufferDelegate method: - (void)captureOutput:(AVCaptureOutput )captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection )connection; 
Then I want to take sampleBuffer and provide it for C++ openalrp library to recognize which takes image bytes or raw pixel data. What function should I use and how to convert sampleBuffer to suitable input type, std::vector<char> or unsigned char* pixelData