+ 3

Can i add elements to the array by using binary operators?

For example, char arr[20] ="xxx" ; arr+="me" ; cout <<arr; is it possible or is there any other way?

4th Apr 2021, 4:50 AM
Jawahirullah
Jawahirullah - avatar
2 Réponses
+ 6
Chronologie There is no push_back() or insert() method on arrays. Note that the OP is talking about arrays and not vector M. Jawahirullah There is no way to use or overload binary operators for arrays. If you only want to add char arrays (strings), then the strcat() function of the <string.h> header will do that for you. Example: ``` char s[10] = "hello"; strcat(s, "bye"); // s now holds "hellobye" ``` In C++, you can just use the string class which has the binary operators overloaded for it. If you want to use += to add elements to a C/C++ array of any type, then I would strongly discourage that because arrays do not grow in size and it is very much possible that you'll set a memory location which does not belong to the array. Just use vector in C++, and use push_back() or pop_back() instead of binary operators.
4th Apr 2021, 5:25 AM
XXX
XXX - avatar
+ 1
not, for it you use the funcions: push_back and insert
4th Apr 2021, 4:58 AM
Alfonso Farías
Alfonso Farías - avatar