c++ - How does vector<string>(const char** first, const char** last...) work when last is given simply as first pl

admin2025-04-19  0

I was looking into converting a raw const char** to a vector and discovered a concise answer here. This works, but I am confused as to how. The vector constructor usage is as follows:

const char* array[] = {"cat", "cows", "crows"};
int count = 3;
vector<string> stringVector(array, array + count);

I assume this is the

template< class InputIt >
vector( InputIt first, InputIt last,
  const Allocator& alloc = Allocator() );

vector constructor form from the docs using a std::string allocator, but how can the pointer arithmetic array + count give us the pointer to the last character string without taking the length of the interim character strings into account? Assuming the character string lengths are taken into account, how/where does this occur?

I would surmise that some 'measuring until null terminator is encountered' was happening under the hood if the params were starting pointer and string count alone, but this form appears to be using direct pointer arithmetic; array + count doesn't seem like it would have enough information to find the actual final character string starting address (unless operator+ has some very fancy footwork for character strings somehow).

I was looking into converting a raw const char** to a vector and discovered a concise answer here. This works, but I am confused as to how. The vector constructor usage is as follows:

const char* array[] = {"cat", "cows", "crows"};
int count = 3;
vector<string> stringVector(array, array + count);

I assume this is the

template< class InputIt >
vector( InputIt first, InputIt last,
  const Allocator& alloc = Allocator() );

vector constructor form from the docs using a std::string allocator, but how can the pointer arithmetic array + count give us the pointer to the last character string without taking the length of the interim character strings into account? Assuming the character string lengths are taken into account, how/where does this occur?

I would surmise that some 'measuring until null terminator is encountered' was happening under the hood if the params were starting pointer and string count alone, but this form appears to be using direct pointer arithmetic; array + count doesn't seem like it would have enough information to find the actual final character string starting address (unless operator+ has some very fancy footwork for character strings somehow).

Share Improve this question edited Mar 6 at 0:01 CCJ asked Mar 5 at 23:51 CCJCCJ 1,75128 silver badges44 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

You don't need to take the lengths of the strings into account. array is an array of pointers, not a 2-dimensional array of characters. array+count is a pointer past the end of the array, equivalent to &array[count].

Note that it would still work if it were a 2-d array, e.g.

#define MAXLENGTH 10
const char array[][MAXLENGTH] = {"cat", "cows", "crows"};

In this case, the size of each element is 10 bytes (the strings will be padded with null bytes), and the pointer arithmetic array + count would multiply by this.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001928a279280.html

最新回复(0)