Using iterators, move the container element n positions to the left (array, vector, list). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

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

C++ visual Studio

9th May 2020, 1:22 PM
Asatbek Xalimjonov
Asatbek Xalimjonov - avatar
3 Answers
+ 4
назад сдвинуть,чтобы назад сдвинуть укажите с минусом)- "; cin>> m; for (auto s : lt) { cout << s<<" "; } cout << endl; sdvig(lt,n,m,result); cout << "после сдвига-" << endl; for (auto s : result) { cout << s << " "; }
9th May 2020, 6:00 PM
Asatbek Xalimjonov
Asatbek Xalimjonov - avatar
+ 3
#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 << "Введите на сколько индексов сдвинуть(можно вперед назад с
9th May 2020, 5:58 PM
Asatbek Xalimjonov
Asatbek Xalimjonov - avatar
+ 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!
9th May 2020, 1:35 PM
Lothar
Lothar - avatar