operator-keyword

Is there any way to optimize c++ string += operator?

我的未来我决定 提交于 2020-08-17 10:39:32
问题 English is not good, but please understand. string str; string str_base = "user name = "; string str_user_input; string str_system_message; ... str = str_base + str_user_input + "\n [system message :]" + str_system_message; cout << str << endl; I'm using the existing code like this, is there a way to optimize string? I think this code does a lot of useless operations. Is there a way to optimize? 回答1: Your question says +=, but you're not using += anywhere. You are using only +. "+" is

Operator Overloading C++: write-only version

有些话、适合烂在心里 提交于 2020-08-05 06:11:28
问题 I am overloading operators for a data structure, so I have the standard function declarations: T & operator[](int i); //used for regular objects const T & operator[](int i) const; // used for const objects So what I want to do is to have a two versions of operator[] for regular objects: one that does something different when the operator[] is used to write rather than read. I have been reading that this is possible, but I have not yet seen any code. I have seen many times this question asked,

C++ - overloading [] operator

别等时光非礼了梦想. 提交于 2020-07-31 12:40:11
问题 I have a template class Array: template <class T=int, int SIZE=10> class Array { T TheArray[SIZE]; public: void Initialize() { for (int idx=0; idx < SIZE; idx++) { TheArray[idx] = T(); } } T& operator [](int idx) { return TheArray[idx]; } T operator [](int idx) const { return TheArray[idx]; } } I have some questions on operator [] overloading (I found this example on net). I understand that T& operator [](int idx) returns a reference to an array value with index idx and that T operator [](int

C++ - overloading [] operator

丶灬走出姿态 提交于 2020-07-31 12:40:06
问题 I have a template class Array: template <class T=int, int SIZE=10> class Array { T TheArray[SIZE]; public: void Initialize() { for (int idx=0; idx < SIZE; idx++) { TheArray[idx] = T(); } } T& operator [](int idx) { return TheArray[idx]; } T operator [](int idx) const { return TheArray[idx]; } } I have some questions on operator [] overloading (I found this example on net). I understand that T& operator [](int idx) returns a reference to an array value with index idx and that T operator [](int

How can I get execution_date in dag?? the outside of operator?

对着背影说爱祢 提交于 2020-07-08 12:26:50
问题 How can I get an execution_date parameter in outside of dag? execution_min = "{{execution_date.strftime('%M') }}" if execution_min == '00': logging.info('**** ' + "YES, It's 00") final_task = DummyOperator( task_id='task_y00', ... dag=dag ) else: logging.info('**** ' + "NOPE!!!") final_task = DummyOperator( task_id='task_n00', ... dag=dag ) I want to set a task stream with dynamically with execution_date (especially minute) But Jinja template won't work with template_fields = ['execution_date

Why in conditional operator (?:), second and third operands must have the same type?

故事扮演 提交于 2020-06-24 03:16:38
问题 Why in conditional operator ( ?: ), second and third operands must have the same type? My code like this: #include <iostream> using std::cout; int main() { int a=2, b=3; cout << ( a>b ? "a is greater\n" : b ); /* expression ONE */ a>b? "a is greater\n" : b; /* expression TWO */ return 0; } When compile it using g++, it issue an error: main.cpp:7:36: error: operands to ?: have different types ‘const char*’ and ‘int’ main.cpp:8:28: error: operands to ?: have different types ‘const char*’ and

C++ operator overloading adding 3 vectors together

我只是一个虾纸丫 提交于 2020-05-17 08:09:37
问题 For now this is how I add 3 vectors of type Product together: vector1.insert(std::end(vector1), std::begin(vector2), std::end(vector2)); vector1.insert(std::end(vector1), std::begin(vector3), std::end(vector3)); How do I use operator overloading (I assume overloading the + and = operators) to simplify my code? Product has the following properties: private: std::string url; double cost; std::string name; std::string site; 回答1: Operating overloading is just a normal free function, or member