How to insert the first and last elements of an array of strings into a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
 
int main()
{
    std::string arr[] = {"c++", "c", "c#", "java", "python", "php", "go"};
    
    int len = sizeof(arr)/sizeof(arr[0]);

    std::vector<std::string> v(arr, arr + 1);
    
    v.push_back(arr[len - 1]);

    for (const auto &str : v) 
        std::cout << str << "\n";        

    return 0;
}
 
 
 
 
 
/*
run:
 
c++
go

*/

 



answered Sep 19, 2021 by avibootz

Related questions

1 answer 202 views
1 answer 170 views
1 answer 176 views
1 answer 182 views
3 answers 162 views
...