This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <string>
#include <iostream>
int main(int argc, char *argv[])
{
if (argc > 1) {
std::string concatenatedArgs;
for (int argnr = 1; argnr != argc; ++ argnr) {
while (*argv[argnr] != '\0') {
concatenatedArgs += *argv[argnr]; // Add one character at a time.
++ argv[argnr]; // Step address forward to the next character.
}
}
std::cout << concatenatedNames << std::endl;
}
return 0;
}
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
Arrays are not CopyAssignable; they can't be passed by value.
Instead, the implicit conversion from array to pointer is applied and the resultant pointer is passed by value.