@soon
2545 / 1310 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
|
28.12.2011, 17:13
|
|

Сообщение от DebieCooepr
1) После максимального элемента списка L вставить заданный элемент a.
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(nullptr));
std::list<int> l(10);
std::generate(l.begin(), l.end(), [] { return rand() % 21; });
std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
int a;
std::cin >> a;
auto max = std::max_element(l.begin(), l.end());
l.insert(++max, a);
std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
} |
|
Без C++0x
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
template <class T> T gen() { return rand() % 21; }
int main()
{
srand(time(NULL));
std::list<int> l(10);
std::generate(l.begin(), l.end(), gen<int>);
std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
int a;
std::cin >> a;
std::list<int>::iterator max = std::max_element(l.begin(), l.end());
l.insert(++max, a);
std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
} |
|
1
|