A use of pointers is the ability to iterate and update a collection, for example:
char list[3]{'a', 'b', 'c'};
char *pointer{list};
pointer++;
*pointer = 'd';
std::cout << list[0] << std::endl;
std::cout << list[1] << std::endl;
std::cout << list[2] << std::endl;
Will result in
a
d
c
Great, but raw pointers and C-style arrays are deprecated in modern C++, in favour of smart pointers, like std::unique_ptr
and smart containers, like std::vector
.
I tried a bit, but with no result, for example:
char list[3]{'a', 'b', 'c'};
std::unique_ptr<char> pointer{std::make_unique<char>(list[0])};
(*pointer)++;
*pointer = 'd';
Will still result in
a
b
c
What am I missing?
And is it a way to use with std::vector
?