stl

why did it compile errorly when i added a reference argument to a member function which is the argument to std::mem_fun()?

ぃ、小莉子 提交于 2019-12-23 20:22:48
问题 Firstly, I had a snippet as following: struct D { int sum; D():sum(0){accum();} void incre(int arg){sum+=arg;} void accum() { int arr[]={1,2,3,4,5}; std::for_each(arr,arr+ sizeof(arr)/sizeof(int), std::bind1st(std::mem_fun(&D::incre),this)); cout << sum <<endl; } }; int main() { D(); } It compiled properly.But after my changing the member function incre to void incre(int & arg){sum+=arg;} it produced errors, like typename _Operation::result_type std::binder1st<_Operation>::operator()

How to read the whole lines from a file (with spaces)?

删除回忆录丶 提交于 2019-12-23 20:08:11
问题 I am using STL. I need to read lines from a text file. How to read lines till the first \n but not till the first ' ' (space)? For example, my text file contains: Hello world Hey there If I write like this: ifstream file("FileWithGreetings.txt"); string str(""); file >> str; then str will contain only "Hello" but I need "Hello world" (till the first \n ). I thought I could use the method getline() but it demands to specify the number of symbols to be read. In my case, I do not know how many

Visual Studio std::stringstream pubsetbuf does not work

≯℡__Kan透↙ 提交于 2019-12-23 20:06:17
问题 pubsetbuf member of std::stringbuf is not working at all in Visual Studio 2010! The code: char *FileData = ... ; unsigned long long FileDataLen = ... ; std::stringstream *SS = new std::stringstream(std::stringstream::in | std::stringstream::out); SS->rdbuf()->pubsetbuf( FileData, (std::streamsize)FileDataLen ); pubsetbuf does nothing in Visual Studio!!! Workaround #1 : std::stringstream *SS = new std::stringstream( std::string(FileData, (size_type)FileDataLen ) ),std::stringstream::in | std:

Only one evaluation guaranteed for std::min/std::max

倾然丶 夕夏残阳落幕 提交于 2019-12-23 19:38:58
问题 Does the C++ standard guarantee that the call c = std::min(f(x), g(x)); evaluates the functions f and g only once? 回答1: Yes. Since std::min is a function, f(x) and g(x) will be evaluated only once. And returned values won't be copied. See the prototype of the function : template<typename T> const T& min ( const T& a, const T& b ); It is a clear difference with preprocessor-genuinely-defined min macro : #define MIN(A,B) ((A)<(B))?(A):(B) 来源: https://stackoverflow.com/questions/3665574/only-one

c++ class with an iterator but no container

霸气de小男生 提交于 2019-12-23 19:00:26
问题 I'm trying to implement a class that would allow me to iterate over objects STL-style without explicitly storing them in a container. A simplified example of this would be, say, a <Paragraph>::iterator in a class that doesn't actually have a container of Paragraphs, but instead has a <string> text variable. It's easy to create a member function that actually goes through text line by line and assemble paragraphs but it seems silly to me to store all this text again in some container just so

Get iterator for const reference

房东的猫 提交于 2019-12-23 18:19:51
问题 I'm developing a class that must return an iterator with the begin() method. Also, I have to develop a function that receives a const reference of this class and iterates over it. When I try to get an iterator from this method, I have the following compilation error: "the object has type qualifiers that are not compatible with the member function." I can't understand why this error appears. Here is the code that I have written: // ------------ class Neuron ------------- class Neuron { ... };

Expanding an STL container into a variadic template

a 夏天 提交于 2019-12-23 18:13:40
问题 To keep things generic and straightforward, say that I have a std::vector of integers, such as: std::vector<int> v; Now, what I am wondering is, is it possible to take n (where n is a constant known at compile time) values from v and pass them to an arbitrary function? I know that this is doable with variadic templates: template<typename... T> void pass(void (*func)(int, int, int), T... t) { func(t...); } And then we hope 'pass' is called with exactly 3 integers. The details don't matter so

Replacing libstdc++.dylib (4.0) global new and delete operators on OSX

老子叫甜甜 提交于 2019-12-23 17:50:38
问题 I'm trying hard to replace the global new and delete operators with XCode 3.2, GCC 4.2, libstdc++ 4.0, dynamic version. I took the protypes directly from the header "new" and implemented them. They are pasted below. The project is a .plugin so a dynamic lib. This plug-in MUST delegate allocation to the main application's own alloc/free routines, which are in an old C SDK. All my own call to new/delete along with std::list and std::map allocations are correctly replaced, BUT not when std:

Replacing libstdc++.dylib (4.0) global new and delete operators on OSX

ぃ、小莉子 提交于 2019-12-23 17:43:34
问题 I'm trying hard to replace the global new and delete operators with XCode 3.2, GCC 4.2, libstdc++ 4.0, dynamic version. I took the protypes directly from the header "new" and implemented them. They are pasted below. The project is a .plugin so a dynamic lib. This plug-in MUST delegate allocation to the main application's own alloc/free routines, which are in an old C SDK. All my own call to new/delete along with std::list and std::map allocations are correctly replaced, BUT not when std:

How to format my own objects when using STL streams?

风流意气都作罢 提交于 2019-12-23 17:32:05
问题 I want to output my own object to a STL stream but with customized formatting. I came up with something like this but since I never used locale and imbue before I have no idea if this makes sense and how to implement MyFacet and operator<<. So my questions are: does this make sense and how to implement MyFacet and operator<< ? The following is a simplified example which shows you what I want to do. struct MyObject { int i; std::string s; }; std::ostream &operator<<(std::ostream &os, const