Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

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 165 views
1 answer 160 views
1 answer 150 views
1 answer 133 views
1 answer 119 views
119 views asked Dec 28, 2020 by avibootz
2 answers 125 views
...