#include<iostream> #include<iomanip> #include<vector> using namespace std; int main(){ vector<int> A(10); int n; int primecount = 0, i, j; cout << "enter a value>=2 as upper limit for prime numbers;"; cin >> n; A[primecount++] = 2; for (i = 3; i < n; i++){ if (primecount == A.size()) A.resize(primecount + 10); if (i % 2 == 0) continue; j = 3; while (j <= i / 2 && i%j != 0) j += 2; if (j>i / 2) A[primecount++] = i; } for (i = 0; i < primecount; i++){ cout << setw(5) << A[i]; if ((i + 1) % 10 == 0) cout << endl; } cout << endl; }
#include<iostream> #include<deque> #include<algorithm> #include<iterator> using namespace std; int main(){ deque<double>values; ostream_iterator<double> output(cout, " "); values.push_front(2.2); values.push_front(3.3); values.push_back(1.1); cout << "values contains:"; for (int i = 0; i < values.size(); i++) cout << values[i] << ' '; values.pop_front(); cout << " after pop_front values contains:"; copy(values.begin(), values.end(), output); values[1] = 5.5; cout << " after values[1]=5.5 values contains:"; copy(values.begin(), values.end(), output); cout << endl; }
#include<iostream> #include<vector> #include<algorithm> #include<iterator> using namespace std; int main(){ int A[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(A) / sizeof(int); vector<int>coll(A, A + N); ostream_iterator<int> output(cout, " "); cout << "list coll contains:"; copy(coll.begin(), coll.end(), output); vector<int>::iterator pos = coll.begin(); cout << " the first element is:" << *pos; vector<int>::reverse_iterator rpos = coll.rbegin(); cout << " the last element is:" << *rpos << endl; back_insert_iterator<vector<int>> iter(coll); *iter = 66; back_inserter(coll) = 88; copy(coll.begin(), coll.end(), output); cout << endl; }
#include<iostream> #include<numeric> using namespace std; int mult(int x, int y){ return x*y; }; int main(){ int a[]={1, 2, 3, 4, 5}; const int n = sizeof(a) / sizeof(int); cout << "the result by multipling all elements in a is:" << accumulate(a, a + n, 1, mult) << endl; }