Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize a vector of strings from an array? If so, how?

So for example, on GeeksForGeeks.org, contributing user "Kartik" offers the following example for initializing a vector of integers:

// CPP program to initialize a vector from 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    int arr[] = { 10, 20, 30 }; 
    int n = sizeof(arr) / sizeof(arr[0]); 

    vector<int> vect(arr, arr + n); 

    for (int x : vect) 
        cout << x << " "; 

    return 0; 
} 

If I understand what I'm reading correctly, sizeof(arr) is some number (which I assume is the length of the array arr; i.e. 3, please correct me if I'm wrong) divided by sizeof(arr[0]) (which I assume to be 1) -- basically just being a roundabout way of saying 3/1 = 3.

At this point, vector<int> vect(arr, arr + n) appears to be a vector of size 3, with all values initialized to arr + n (which I'm assuming is a way of saying "use the 3 items from arr to instantiate; again, please correct me if I'm wrong).

Through whatever sorcery, the output is 10 20 30.

Now, regardless of whether or not any of my above rambling is coherent or even remotely correct, my main question is this: can the same technique be used to instantiate some example vector<string> stringVector such that it would iterate through strings designated by some example string stringArray[] = { "wordA", "wordB", "wordC" }? Because, as I understand it, strings have no numeric values, so I imagine it would be difficult to just say vector<string> stringVector(stringArray, stringArray + n) without encountering some funky junk. So if it is possible, how would one go about doing it?

As a rider, why, or in what type of instance, would anyone want to do this for a vector? Does instantiating it from an array (which as I understand it has constant size) defeat the purpose of the vector?

Just as a disclaimer, I'm new to C++ and a lot of the object-oriented syntax involving stuff like std::vector<_Ty, _Alloc>::vector...etc. makes absolutely no sense to me, so I may need that explained in an answer.

To whoever reads this, thank you for taking the time. I hope you're having a good day!

like image 328
Nicholas Jarecki Avatar asked Dec 30 '22 17:12

Nicholas Jarecki


1 Answers

Clarifications:

  • sizeof(arr): returns the size in bytes of the array, which is 12 because it has 3 ints, and each int in most implementations has a size of 4 bytes, so 3 bytes x 4 = 12 bytes.
  • sizeof(arr[0]): returns the size in bytes of the first element of the array, which is 4 because it is an int array.
  • vector<int> vect(arr, arr + n): the vector class has multiple constructors. Here we are not using the constructor you are thinking of. We are using a constructor that takes begin and end iterators for a range of elements, making a copy of those elements. Pointers can be used as iterators, where in this case arr is the begin iterator and arr + n is the end iterator.

Note: int* + int returns int*.

Note: We should also consider that the "end" of an array is a pointer to the next space after the last item in the array, and the constructor will copy all the items except the item past the end.

link

Answer:

Yes, remember that here, the constructor is taking iterators, not any item of the array, so we can do it easily like this with little changes:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    // changed int to string and the array values
    string arr[] = { "one", "two", "three" };
    int n = sizeof(arr) / sizeof(arr[0]);

    // changed int to string
    vector<string> vect(arr, arr + n);
    
    // changed int to string
    for (string x : vect)
        cout << x << " ";
    return 0;
}
like image 182
DonLarry Avatar answered Jan 11 '23 22:01

DonLarry