std

C++ Call pointer to member with a map from a const function

99封情书 提交于 2019-12-12 02:09:50
问题 I have a map of pointer to member declared as : std::map<char, T (Operand::*)(const T &, const T &)> op_map; I fill my map with pointer to member directly in the constructor of my class with : op_map['+'] = &Operand::op_add; For example, op_add source code is : T op_add(const T & a, const T & b) { return a + b; } And I want to call my pointer to member from a const function. Here is the source code : IOperand *res_int32(char op, const IOperand & rhs) const { IOperand *res = const_cast

Is it safe to pass an unconstructed buffer to the constructor of std::ostream?

Deadly 提交于 2019-12-11 23:09:34
问题 This is a convenient way to define a subclass of ostream that sets a buffer (MyStreamBuf is some subclass of std::streambuf): class MyOStream : public std::ostream { public: MyOStream(): std::ostream(&buffer) {} // ... private: MyStreamBuffer buffer; }; Here std::ostream's constructor is called before the constructor of buffer, so the streambuf* being passed in is pointing to an unconstructed object that has virtual methods. The same issue applies in ~MyStream(), as buffer is destructed

Ignoring commas in c++ cin

余生长醉 提交于 2019-12-11 23:01:41
问题 I have the following code: float x1 = 0,x2 = 0,y1 = 0,y2 = 0; cout << "Enter coordinates as \"(x1,y1) (x2,y2)\"\n"; cin >> x1; cin.ignore(1, ','); cin >> y1; cin >> x2; cin.ignore(1, ','); cin >> y2; cout << "Coordinates registered as (" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ").\n"; But this always returns (0,0) (0,0). What would the correct implementation of cin.ignore be? 回答1: If you are literally entering in the data in the form of (x1,y1) (x2,y2) like (10,20) (30,40) Then you

C++ error C2893

一个人想着一个人 提交于 2019-12-11 20:22:41
问题 ---Answered --> Make the operator functions const! I am writing a template and keep getting the following error: Error 1 error C2893: Failed to specialize function template 'unknown-type std::less::operator ()(_Ty1 &&,_Ty2 &&) const' when trying to use the template with a class, even if the class has overloaded operators. Note that the template does work with primitives ____TEMPLATE_________ #include <vector> #include <algorithm> using namespace std; template <class T> class Set{ int elements

Issues with a C++ Test Project in Visual Studio 2010

ⅰ亾dé卋堺 提交于 2019-12-11 18:51:42
问题 I have been having problems making a C++ Test Project work in Visual Studio 2010. I am looking for what is wrong/what to do about it. This kind of test project has to be compiled as managed code. No problem. C++ can be built that way. I did it this trivial example std::list example. I created a C++ CLR project. I pasted this code in and compiled. I can step through the code as it runs. It works. #include "stdafx.h" #include <list> using namespace System; int main(array<System::String ^> ^args

How can we manipulate std class in php?

こ雲淡風輕ζ 提交于 2019-12-11 18:31:57
问题 Is it possible to add elements to stdclasses like we do for arrays? Array ( [0] => item 1 [1] => item 2 ) Stdclass ( [0] => item 1 [1] => item 2 ) Is it generally harder to manipulate objects as compared to arrays? Since we have lots of array functions to make use of etc. 回答1: Sure, you can add new elements... $Object = new StdClass(); $Object->item1 = 1; $Object->item2 = 2; If you want to iterate object as array you should use PHP SPL ArrayIterator or RecursiveArrayIterator. Also you can use

Winapi GetOpenFileName Extension Filter not working

可紊 提交于 2019-12-11 17:58:12
问题 I'm trying to apply file's extension filters to the file's selection dialog. This way works: ofn.lpstrFilter = "(*.exe) Windows Executable\0*.exe\0" "(*.ini) Windows Initialization file \0*.ini\0" "(*.dll) Dynamic Link Library \0*.dll\0" "(*.lib) Windows Library file \0*.lib\0" "(*.conf) Windows Configuration file \0*.conf\0"; But when I'm assigning extension filters dynamically, via parameters, it fails, filters don't appear in the combo box: LPCSTR filter = (LPCSTR)extFilter; //Contains

Reading and appending from/to a file with std::fstream

≯℡__Kan透↙ 提交于 2019-12-11 17:26:19
问题 I'm wondering why the following piece of code doesn't work, looks pretty straight-forward, am I making a mistake? The result of this is: file created but empty, if I manually add lines those lines are showed with this code, but nothing else happens. #include <fstream> #include <iostream> #include <string> using namespace std; int main(){ fstream mfile("text.txt", ios_base::in | ios_base::out | ios_base::app); mfile.seekg(ios_base::beg); string line; while( getline(mfile,line) ){ std::cout <<

C++ std::regex using lookahead fails

巧了我就是萌 提交于 2019-12-11 15:01:31
问题 I need to parse a txt file, from disk. So i have firstly made an example, to test regex. This is my example code: std::string txt("paragraph:\r\nthis is the text file\r\ni need only this data\r\nnotthis"); std::smatch m; std::regex rt("paragraph:([\\S\\s](?=notthis))"); std::regex_search(txt, m, rt); std::cout << m.str(1) << std::endl; So i'm trying to parse until notthis , but returned match m is a failed match. If i do the regex like this: std::regex rt("paragraph:([\\S\\s]+)"); it works

Copying from std container frm arbitrary source object

旧时模样 提交于 2019-12-11 14:56:24
问题 I created a read only iterator which allows me to use it in a for loop more conveniently then with the std iterators, similar to what boost does with the FOREACH (not as good but good enough :)) Now it looks like this: for(ReadOnylIterator<MyClass *> class = parent.getIterator(); class.end(); ++class) class->function(); The problem that I have now is, that I must implement a function on the parent which returns the iterator. Since the std containers have all the same syntax, I was wondering