std::vector<char> v; v.push_back('a'); v.push_back('b'); v.push_back('c'); v.push_back('d'); v.push_back('e'); v.push_back('f'); char c[3] = { 'z', 'x', 'y' }; // Want to make abzxyf //v.insert(v.begin() + 2, c, c + 3); // it doesn't work as I wanted. // Yes it works. but if c is more bigger, it will be crash. std::copy(c, c + 3, v.begin() + 2); v.clear(); v.push_back('a'); v.push_back('b'); v.push_back('c'); v.push_back('d'); v.push_back('e'); v.push_back('f'); // If vector needs more memory, I'd let him grow automactically // So I tried this.(expected abcdezxy) // But it result is abcdezxyf. f is still remain. std::copy(c, c + 3, std::inserter(v, v.begin() + 5));
Which algorithm or method should be used?
If sizeof(c) larger, resize() to copy() , which should do the trick.
sizeof(c)
resize()
copy()
eg.
if (sizeof(c) + 2 > v.size()) v.resize(sizeof(c) + 2); // now copy std::copy(c, c + sizeof(c), v.begin() + 2);
If you want to do text processing, you can use std::string , which has replace functions.
std::string
replace
std::vector no. You must use the appropriate combination of rewriting elements in combination with insert and erase .
std::vector
insert
erase
Source: https://habr.com/ru/post/893065/More articles:Where is the rabbit spring XSD (layout for rabbit space: namespace) - springHow to clear L1 and L2 processor cache - cachingHow to clear the processor cache in Linux from program C? - cHow to perform cache operations in C ++? - c ++How is cast from char * to T *? - c ++Python for C # AES CBC PKCS7 - pythonA combination of all possible string cases - pythonWhat is the use of a partial class? - c #memory pool questions - c ++ListView switch not showing - androidAll Articles