std

std::sort functor one line

瘦欲@ 提交于 2019-12-11 10:24:29
问题 I have declared a functor and made a call so std::sort with that functor as a parameter. Code: struct { bool operator() (const CString& item1, const CString& item2){ return MyClass::Compare( Order(_T("DESC")), item1, item2); } }Comparer; std::sort(AllObjects.GetData(), AllObjects.GetData() + AllObjects.GetSize(), Comparer); Simple question: can I do this in one line? 回答1: If your compiler supports c++11, you can use a lambda std::sort(AllObjects.GetData(), AllObjects.GetData() + AllObjects

Is c_str() on a concatenated string literal safe?

为君一笑 提交于 2019-12-11 10:14:25
问题 I know from this answer that string literals are allocated statically. But is the following string concatenation also safe to use? void someFunction(std::string& foo) { functionTakingCString(("start " + foo + " end").c_str()); } Follow Up Question : As stated in the comments, this would be indeed unsafe when functionTakingCString would store that pointer. In this case, would the following be valid: void someFunction(std::string& foo) { std::string bar = "start " + foo + " end";

C++ custom collection reverse_iterator with similar behaviour to std::vector implementation

南楼画角 提交于 2019-12-11 10:08:50
问题 I have a template based custom collection (as we cannot use std::vector on the interface). I would like to implement a reverse_iterator specific to this collection. The reverse iterator struct below is a structure nested within the collection class. An iterator (basically a pointer to element type of the collection) is already implemented. This is my first attempt at a reverse iterator. template <typename T> struct reverse_iterator { typedef T::iterator iterator; typedef T& reference; inline

How to split a hex string into std::vector?

一曲冷凌霜 提交于 2019-12-11 10:08:50
问题 Given a hex std::string like "09e1c5f70a65ac519458e7e53f36" , how can I split it in chunks of two digits and store them into an std::vector<uint8_t> ? I loop over the string in steps of the chunk size, but I don't know how to convert the hex chunk into a number. This is what I have so far. vector<byte> vectorify(string input, int chunk = 2) { vector<uint8_t> result; for(size_t i = 0; i < input.length(); i += chunk) { int hex = input.substr(i, chunk); // ... } return result; } 回答1: #include

How to find the depth of each node in std::map?

五迷三道 提交于 2019-12-11 10:08:31
问题 If I construct, my own binary tree, then I can find the depth of each node. The sample code is as follows template<class datatype> void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth) { if ( left ) left->printNodeWithDepth(currentNodeDepth+1); std::cout << value << " and the depth is " << currentNodeDepth << std::endl; if ( right) right->printNodeWithDepth(currentNodeDepth+1); } But wondering, since map is a b-tree, is it possible to write something similar to this for a std:

How to get the accurate length of a std::string?

你离开我真会死。 提交于 2019-12-11 09:45:08
问题 I am trimming a long std::string to fit it in a text container using this code. std::string AppDelegate::getTrimmedStringWithRange(std::string text, int range) { if (text.length() > range) { std::string str(text,0,range-3); return str.append("..."); } return text; } but in case of other languages like HINDI "हिन्दी" the length of std::string is wrong. My question is how can i retrieve accurate length of the std::string in all test cases. Thanks 回答1: Assuming you're using UTF-8, you can

Error: ‘log2’ is not a member of ‘std’

China☆狼群 提交于 2019-12-11 09:34:42
问题 I am trying to compile a project using make command. I have several occured errors about namespace std::. I am using Ubuntu and I think I've installed all I need to compile C++ language. Here's the code: #include "texture.hpp" #include "texturefetch.hpp" #include <png.h> #include <cstdio> #include <iostream> #include <cmath> #include <assert.h> #include <string> Texture::~Texture(){ delete[] mPixels; for (int i=1; i<mlodmax+1; ++i) delete[] mMipMapLevels[i]; delete[] mMipMapLevels; } bool

Best way to replace a set in C++

穿精又带淫゛_ 提交于 2019-12-11 09:10:26
问题 I have to refactor old code that looks like this (I am not a very proficient C++ coder) std::set<SomeObject>::iterator it = setobject.begin(); do { it->setProperty1ToNextValue(); it->getProperty2(); it->getProperty3(); it++ } while (it != setobject.end()); Basically I want to iterate through the elements of the set and get and set/update some of their properties. I cant use the original set since I run in the problems described in this thread the object has type qualifiers that are not

Selecting a random element for a specific key in a Multimap

ε祈祈猫儿з 提交于 2019-12-11 07:54:56
问题 How can I select a random element for a specific keys in a multimap. For example: multimap<string, string> map; map.insert(pair<string, string>("Mammal", "Tiger")); map.insert(pair<string, string>("Mammal", "Chicken")); map.insert(pair<string, string>("Mammal", "Fox")); map.insert(pair<string, string>("Fish", "Clown Fish")); map.insert(pair<string, string>("Fish", "Ray")); In the above, what would be the best way to get a random "Mammal"? I know I can get the iterators for the "Mammal" so:

Why float “division-by-zero” exception wasn't caught in a function even handler was set?

不羁岁月 提交于 2019-12-11 07:36:16
问题 I've tried to learn the signal handling in C, when found strange behaviour. When x /= y; executed in the context of the main function the signal handler works. But when the same executed in some function (bad_func) handler is ignored however signal handler for SIGFPE is already set. Q: Why SIGFPE wasn't caught in a function by my global signal handler even _control87 was called? (MS VC 2010): #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <setjmp.h>