std

C++ std::thread of a member function

為{幸葍}努か 提交于 2019-12-18 04:19:11
问题 I'm trying to program a command line server that would receive information from a serial port, parse it, and record it in an internal object. Then upon request from a client the server would return the requested information. What I want to do is put the receiver & parser parts in a separated thread in order to have the server running along side, not interfering with the data collection. #include <iostream> #include <thread> class exampleClass{ std::thread *processThread; public void

Erasing item in a for(-each) auto loop

怎甘沉沦 提交于 2019-12-18 04:18:12
问题 Is there a way to erase specific elements when using a auto variable in a for loop like this? for(auto a: m_Connections) { if(something) { //Erase this element } } I know I can either do say for(auto it=m_map.begin() ... or for(map<int,int>::iterator it=m_map.begin() ... and manually increment the iterator (and erase) but if I could do it with less lines of code I'd be happier. Thanks! 回答1: No, there isn't. Range based for loop is used to access each element of a container once. Every time an

How much performance difference when using string vs char array?

假如想象 提交于 2019-12-18 04:18:09
问题 I have the following code: char fname[255] = {0} snprintf(fname, 255, "%s_test_no.%d.txt", baseLocation, i); vs std::string fname = baseLocation + "_test_no." + std::to_string(i) + ".txt"; Which one performs better? Does the second one involve temporary creation? Is there any better way to do this? 回答1: Let's run the numbers: The code (I used PAPI Timers) main.cpp #include <iostream> #include <string> #include <stdio.h> #include "papi.h" #include <vector> #include <cmath> #define TRIALS

How do I strip a tuple<> back into a variadic template list of types?

孤人 提交于 2019-12-18 03:19:29
问题 Is there a way to strip a std::tuple<T...> in order to get it back to T... ? Example Suppose vct<T...> is a pre-existing variadic class template , using U = std::tuple<int,char,std::string>; using X = vct<int,char,std::string>; using Y = vct< strip<U> >; // should be same as X Notes I know about std::tuple_element, but I need all the elements, in a form that is usable as T... For reference, I have found this question, which is similar, but my needs are somewhat simpler (so I hope there is a

Add same value multiple times to std::vector (repeat)

空扰寡人 提交于 2019-12-18 03:04:21
问题 I want to add a value multiple times to an std::vector. E.g. add the interger value 1 five times to the vector: std::vector<int> vec; vec.add(1, 5); vec should be of the form {1,1,1,1,1} afterwards. Is there a clean c++ way to do so? 回答1: It really depends what you want to do. Make a vector of length 5, filled with ones: std::vector<int> vec(5, 1); Grow a vector by 5 and fill it with ones: std::vector<int> vec; // ... vec.insert(vec.end(), 5, 1); Or resize it (if you know the initial size):

Android std and stl support

心已入冬 提交于 2019-12-17 22:24:36
问题 I am playing with android ndk. I am using Window Vista with cygwin (latest version). I compiled and launched the hello world jni sample on my phone. It is working. The code is (is a .cpp file): #include <string.h> #include <jni.h> extern "C" { JNIEXPORT jstring JNICALL Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis); }; jstring Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { return env-

C++ When is it OK to extend the `std` namespace?

有些话、适合烂在心里 提交于 2019-12-17 21:59:53
问题 A thread on SO says that extending std is UB (ok, unless you're the standard writers of course). But from time to time, std is happily extended. When is it OK to do so? 回答1: The only case where it is OK to add a definition into the std namespace is specialization of a template that already exists in the namespace and to explicitly instantiate a template. However, only if they depend on a user defined type. [namespace.std] (standard draft): The behavior of a C++ program is undefined if it adds

std::mem_fun vs std::mem_fn

拜拜、爱过 提交于 2019-12-17 21:55:46
问题 What is the difference between std::mem_fun and std::mem_fn ? Why is the naming so confusing? Boost's documentation says that std::mem_fn can replace std::mem_fun in most cases. So in what situation would you still use std::mem_fun ? 回答1: std::mem_fun is deprecated. std::mem_fn can do everything it does, and it does it more conveniently. The relation between the two is the same as the relation between std::bind1st / std::bind2nd and the C++11 std::bind . Both std::mem_fn and std::bind were

Design of std::ifstream class

不羁岁月 提交于 2019-12-17 20:49:02
问题 Those of us who have seen the beauty of STL try to use it as much as possible, and also encourage others to use it wherever we see them using raw pointers and arrays . Scott Meyers have written a whole book on STL, with title Effective STL. Yet what happened to the developers of ifstream that they preferred char* over std::string . I wonder why the first parameter of ifstream::open() is of type const char* , instead of const std::string & . Please have a look at it's signature: void open

Not including stdlib.h does not produce any compiler error!

元气小坏坏 提交于 2019-12-17 20:25:35
问题 Hopefully this is a very simple question. Following is the C pgm (test.c) I have. #include <stdio.h> //#include <stdlib.h> int main (int argc, char *argv[]) { int intValue = atoi("1"); double doubleValue = atof("2"); fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, doubleValue); return 0; } Note that I am using atoi() and atof() from stdlib.h, but I do not include that header file. I compile the pgm (gcc test.c) and get no compiler error! I run the pgm (./a.out) and