How to raise of all items of a vector to the power of N in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <iterator>
#include <cmath>

template <typename T>
void PrintVector(std::vector<T> &arr) {
    copy(arr.begin(), arr.end(), std::ostream_iterator<T>(std::cout," "));
}

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
    
    int N = 3;
    for (auto &item : v) {
        item = pow(item, N);
    }
    
    PrintVector(v);
    
    return 0;
}



/*
run:

1 8 27 64 125 216 343 

*/

 



answered May 8, 2021 by avibootz

Related questions

2 answers 176 views
1 answer 177 views
1 answer 165 views
1 answer 147 views
1 answer 130 views
130 views asked Dec 28, 2020 by avibootz
2 answers 136 views
...