Axborot texnologiyalari va kommunikatsiyalarni rivojlantirish va
Qo'shish funktsiyasi Ushbu usul, nomidan ko'rinib turibdiki, elementni ma'lum bir joyga, ro'yxatga kiritadi. Insert () ning uchta o'zgaruvchilari mavjud, ular quyidagicha:
insert (iterator, element): iterator ko'rsatgan pozitsiyadan oldin ro'yxatdagi elementni qo'shadi.
insert (iterator, count, element): ro'yxatga elementni iterator ko'rsatgan pozitsiyadan oldin qo'shadi, necha marta hisoblash.
insert (iterator, start_iterator, end_iterator): start_iterator ko'rsatgan elementni end_iterator ko'rsatgan elementga iterator ko'rsatgan joydan oldin joylashtirish
#include
#include using namespace std;
int main()
{
list l = {1,2,3,4,5};
list::iterator it = l.begin();
list new_l = {10,20,30,40}; // new list
new_l.insert (new_l.begin(), l.begin(), l.end());
/* insert elements from beginning of list l to end of list l
before 1 position in list new_l */
/* now the list new_l is 1 100 2 3 4 5 10 20 30 40 */
l.insert(l.begin(), 5, 10); // insert 10 before beginning 5 times
/* now l is 10 10 10 10 10 1 100 2 3 4 5 */
for (int x : new_l) {
std::cout << x << '\n';
}
return0;
}
push_back va push_front funktsiyalari
push_back (element) usuli elementlarni orqa tomondan ro'yxatga kiritish uchun ishlatiladi.
push_front (element) usuli elementlarni ro'yxatdan old tomondan surish uchun ishlatiladi.
#include
#include #include int main()
{
// Create a list containing integers
std::list l ={7, 5, 16, 8};
// Add an integer to the front of the list
l.push_front(25);
// Add an integer to the back of the list
l.push_back(13);
// Insert an integer before 16 by searching
auto it =std::find(l.begin(), l.end(), 16);
if(it != l.end()){
l.insert(it, 42);
}
// Iterate and print values of the list
for(int n : l){
std::cout<< n <<'\n';
}
}