Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Using copy_n
//Copy #include <iostream> #include <list> #include <algorithm> #include <cmath> using namespace std; template<class T>T value(const T& x){ return pow(x, 2) + 3*x + 1; }; int main() { const int sz1 = 15; const int sz2 = 20; const int sz3 = 30; list<int>L1; list<int>L2; list<int>L3; int x = 0; int y = 0; for(int i = 0; i < sz1; i++){ y = value(x); L1.push_front(y); ++x; } cout << "\nL1\t[ "; for(auto i : L1) cout << i << " "; cout << "]\n"; L2.assign(L1.begin(), L1.end()); cout << "L2:\t[ "; for(auto i : L2) cout << i << " "; cout << "]\n"; //std::copy_n // copy_n(L1.begin(), 15, inserter(L3, L3.begin())); cout << "L3:\t[ "; for(auto i : L3) cout << i << " "; cout << "]\n"; // Adjust to the subsequence size // int n = 10; // number of elements to copy (subswquence size) list<int>L4; (n > L1.size()) ? (n = L1.size()) : n; // must not go beyond the size of the source int val = value(n-1); // value at n-1 list<int>::iterator LIter1; LIter1 = find(L1.begin(), L1.end(), val); // use the algorithm 'find' to locate the value 'val' in list copy_n(LIter1, n, inserter(L4, L4.begin())); // use the iterator as the start point of sequence to copy cout << "\nvalue = " << val << "\tnumber of elements = " << n << "\n\n"; cout << "L4:\t[ "; for(auto i : L4) cout << i << " "; cout << "]\n"; }
run
|
edit
|
history
|
help
0
typeid
StackQuiz
break.cpp
Binary search2
Extended Euclidean Algorithm
MapTel2
MyStack
1568-1644 Solar eclipses, make 1617(8) and 1620(12) Lunar eclipses the same as Chinese records
Hello
motores