Using iterators, move the container element n positions to the left (array, vector, list). | SoloLearn: Learn to code for FREE!

+2

Using iterators, move the container element n positions to the left (array, vector, list).

5/9/2020 1:22:23 PM

Asadbek Xalimjonov

3 Answers

New Answer

+2

назад сдвинуть,чтобы назад сдвинуть укажите с минусом)- "; cin>> m; for (auto s : lt) { cout << s<<" "; } cout << endl; sdvig(lt,n,m,result); cout << "после сдвига-" << endl; for (auto s : result) { cout << s << " "; }

+1

It would be great if you could share the attempts you have made with us. Please put your code in playground and link it here. Thanks!

+1

#include<iostream> #include<algorithm> #include<array> #include<vector> #include<list> #include<functional> #include<stdlib.h> #include<stdafx.h> using namespace std; template <class T,class D> int sdvig(T &a,int n,int m,D &res) { int replace=0; int move=n+m; int l = m; for (auto it=a.begin();it!=a.end();it++) { if (n == 0) { replace = *it; goto metka; } res.push_back(*it); metka: n--; } if (replace != 0) { if (move > res.size()) { if (l < 0) { auto a = res.begin(); res.insert(a, replace); } else { res.push_back(replace); } } else if(move<=res.size()) { auto itr = res.begin() + move; res.insert(itr, replace); } } else cout << "error"; return 0; } int main() { setlocale(LC_ALL, "rus"); int n; int m; array<int, 5> ar {1,2,3,5,1 }; vector<int> result; vector<int> vec{10,32,2,6,1,8,9}; list<int> lt{ 1,2,5,20,12,42,32 }; cout << "Введите индекс массива- "; cin >> n; cout << "Введите на сколько индексов сдвинуть(можно вперед назад с