stl

Initialize a container with iterator range of container with different type

夙愿已清 提交于 2020-01-03 15:21:13
问题 Let's say we have std::set<int> and we want to create a std::vector<int> with all values from that set: std::set<int> set; std::vector<int> vec( set.begin(), set.end() ); This is simple and elegant. But let's say I have a std::map<std::string,int> and I want to copy all values to std::vector<int> . Unfortunately there is no constructor, that accepts range of iterators and converter function. Why there is no such constructor provided? Is there another simple and elegant way to initialize one

How can I redirect a std::ofstream to std::cout?

假如想象 提交于 2020-01-03 14:18:11
问题 I'm working with some code that uses a global debug logger that is of type std::ofstream* . I would like to redirect this to std::cout since I'm using the code in realtime, as opposed to a batch method for which it was designed. Is it possible to redirect the global std::ofstream* pointer it uses to std::cout ? I know std::ofstream inherits from std::ios , which allows one to change the stream buffer using the rdbuf() method, but unfortunately it appears std::ofstream redefines the rdbuf()

Will the standard library of C++11 have forward declaration headers?

不问归期 提交于 2020-01-03 12:31:09
问题 In C++03 there are no <vectorfwd> -like files, while there is the <iosfwd> header. Will this change in the future? It could be valuable to reduce dependencies and for better modularity. UPDATE: I have received an answer from the language creator: " practically it's not possible to require this from all STL vendors ". 回答1: No, there are no new forward files, just the old <iosfwd> . The complete I/O-system is large compared to a vector, so the advantage would perhaps not be that significant. 来源

How can I read binary data from wfstream?

陌路散爱 提交于 2020-01-03 11:57:26
问题 I have a slight problem reading data from file. I want to be able to read wstring's, aswell as a chunk of raw data of arbitrary size (size is in bytes). std::wfstream stream(file.c_str()); std::wstring comType; stream >> comType; int comSize; stream >> comSize; char *comData = new char[comSize]; memset(comData, 0, comSize); stream.read(comData, comSize); //error C2664 : 'std::basic_istream<_Elem,_Traits>::read' // : cannot convert parameter 1 from 'char *' to 'wchar_t *' Perhaps I am using

Fastest Way to Determine if Character Belongs to a Set of Known Characters C++

感情迁移 提交于 2020-01-03 11:33:30
问题 Given any character, what's the fastest way for me to determine if that character belongs to a set (not the container-type) of known characters. In other words, what's the fastest elegant way to implement the conditional: char c = 'a'; if(c == ch1 || c == ch2 || c == ch3 ...) // Do something... Is there an STL container (I'm thinking it might be unordered_set?) that I can just pass the character as a key to and it'll return true if the key exists? Anything with an O(1) lookup time will work

Fastest Way to Determine if Character Belongs to a Set of Known Characters C++

☆樱花仙子☆ 提交于 2020-01-03 11:33:09
问题 Given any character, what's the fastest way for me to determine if that character belongs to a set (not the container-type) of known characters. In other words, what's the fastest elegant way to implement the conditional: char c = 'a'; if(c == ch1 || c == ch2 || c == ch3 ...) // Do something... Is there an STL container (I'm thinking it might be unordered_set?) that I can just pass the character as a key to and it'll return true if the key exists? Anything with an O(1) lookup time will work

emplace_back and VC++ frustration

北慕城南 提交于 2020-01-03 10:20:14
问题 I'm using Visual Studio 2012, trying this both with the default compiler and the Nov CTP compiler, and the following below shows my problem: struct doesCompile { int mA, mB, mC, mD, mE; doesCompile(int a, int b, int c, int d, int e) : mA(a), mB(b), mC(c), mD(d), mE(e) { } }; struct doesNotCompile { int mA, mB, mC, mD, mE, mF; doesNotCompile(int a, int b, int c, int d, int e, int f) : mA(a), mB(b), mC(c), mD(d), mE(e), mF(f) { } }; int _tmain(int argc, _TCHAR* argv[]) { std::vector<doesCompile

Alternative version of find_if which finds all, not just the first?

孤者浪人 提交于 2020-01-03 09:48:11
问题 Is there an alternative version of std::find_if that returns an iterator over all found elements, instead of just the first one? Example: bool IsOdd (int i) { return ((i % 2) == 1); } std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); std::vector<int>::iterator it = find_if(v.begin(), v.end(), IsOdd); for(; it != v.end(); ++it) { std::cout << "odd: " << *it << std::endl; } 回答1: You can just use a for loop: for (std::vector<int>:iterator it = std::find_if(v

std::list threading push_back, front, pop_front

蹲街弑〆低调 提交于 2020-01-03 09:38:07
问题 Is std::list thread safe? I'm assuming its not so I added my own synchronization mechanisms (I think i have the right term). But I am still running into problems Each function is called by a separate thread. Thread1 can not wait, it has to be as fast as possible std::list<CFoo> g_buffer; bool g_buffer_lock; void thread1( CFoo frame ) { g_buffer_lock = true ; g_buffer.push_back( frame ) ; g_buffer_lock = false; } void thread2( ) { while( g_buffer_lock ) { // Wait } // CMSTP_Send_Frame *

C4503 warnings? How do i solve/get rid of them?

别说谁变了你拦得住时间么 提交于 2020-01-03 08:29:34
问题 It's my first time trying out C++ STL. I'm trying to build a multidimensional associative array using map. For example: typedef struct DA { string read_mode; string data_type; void *pValue; void *pVarMemLoc; }DA; int main() { map<string, map<string, map<string, map<string, map<string, DA*>>>>> DATA; DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom1"] = new DA; DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom2"] = new DA; DATA["lvl1"]["stg1"]["flr1"]["dep1"]["rom3"] = new DA; IEC["lvl1"]["stg1"]["flr1"][