Combining a vector of strings

后端 未结 4 2021
忘掉有多难
忘掉有多难 2020-12-24 14:11

I\'ve been reading Accelerated C++ and I have to say it\'s an interesting book.

In chapter 6, I have to use a function from to concatenate from a v

4条回答
  •  难免孤独
    2020-12-24 14:46

    Assuming this is question 6.8, it doesn't say you have to use accumulate - it says use "a library algorithm". However, you can use accumulate:

    #include 
    
    int main () {
        std::string str = "Hello World!";
        std::vector vec(10,str);
        std::string a = std::accumulate(vec.begin(), vec.end(), std::string(""));
        std::cout << a << std::endl;
    }
    

    All that accumulate does is set 'sum' to the third parameter, and then for all of the values 'val' from first parameter to second parameter, do:

    sum = sum + val
    

    it then returns 'sum'. Despite the fact that accumulate is declared in it will work for anything that implements operator+()

提交回复
热议问题