template <typename T> void sequentialWriteToFront(vector<T> & v)
{
auto start = chrono::steady_clock::now();
for (long i =0; i < 100000; i++) {
v.insert(v.begin(), i);
}
auto end = chrono::steady_clock::now();
cout << "VectorSWF:Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
}
vector<long>
sequentialWriteToEnd: 100ms
sequentialWriteToFront: 229611ms
Why?
sequentialWriteToEnd
for vector
requires copy
operation.
| 1 |
| 1 | 1 | copy (with a possible allocation)
| 2 | 1 |
template <typename T> void sequentialWriteToEnd(list<T> & v) // T is long here.
{
auto start = chrono::steady_clock::now();
for (long i =0; i < 1000000; i++) {
v.push_back(i);
}
auto end = chrono::steady_clock::now();
cout << "VectorSWE:Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
}
template <typename T> void sequentialWriteToFront(list<T> & l)
{
auto start = chrono::steady_clock::now();
for (long i =0; i < 100000; i++) {
l.push_front(i);
}
auto end = chrono::steady_clock::now();
cout << "ListSWF:Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
}
template <typename T> void sequentialWriteToEnd(deque<T> & d)
{
auto start = chrono::steady_clock::now();
for (long i =0; i < N; i++) {
d.push_back(i);
}
auto end = chrono::steady_clock::now();
cout << "DequeSWE:Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
}
template <typename T> void sequentialWriteToFront(deque<T> & l)
{
auto start = chrono::steady_clock::now();
for (long i =0; i < 1000000; i++) {
l.push_front(i);
}
auto end = chrono::steady_clock::now();
cout << "DequeSWF:Elapsed time in milliseconds : "
<< chrono::duration_cast<chrono::milliseconds>(end - start).count()
<< " ms" << endl;
}
list<long>
sequentialWriteToEnd: 3ms
sequentialWriteToFront: 4ms
deque<long>
sequentialWriteToEnd: 3ms
sequentialWriteToFront: 7ms
sequentialWriteToFront
for list
and deque
does not require copy
operation.
template <typename T> void sequentialRead(vector<T> &);
template <typename T> void sequentialRead(deque<T> &);
template <typename T> void sequentialRead(list<T> &);
iterator
and dereferencing
(*
) to traverse the list elements. For a list l
, use auto it = l.begin();
vector
template <typename T> void sequentialRead(vector<T> &);
int main(int argc, const char * argv[]) {
vector<long> myVector;
sequentialWriteToEnd(myVector);
sequentialRead(myVector);
return 0;
}
Sample code: https://tinyurl.com/5n8nd4wd
list
template <typename T> void sequentialRead(list<T> &);
int main(int argc, const char * argv[]) {
list<long> myList;
sequentialWriteToEnd(myList);
sequentialRead(myList);
return 0;
}
Sample: https://tinyurl.com/2p8nveba
deque
template <typename T> void sequentialRead(deque<T> &);
int main(int argc, const char * argv[]) {
deque<long> myDeque;
sequentialWriteToEnd(myDeque);
sequentialRead(myDeque);
return 0;
}
sequentialRead
for vector
, list
and deque
template <typename T> void sequentialRead(const T & C){
}
c
randomREF:random
is related to OS security.
Old random function rand() in cstdlib
generates a pseudo-random integral number between 0 and RAND_MAX.
#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
int main(){
srand(static_cast<int> (time(NULL)));// Reset random sequence.
for(int i=0;i<10;++i)
cout<<rand()<<endl; //Generate random number from 0 to RAND_MAX.
}
Sample: https://tinyurl.com/ysatktnt
srand
static u_long next = 1;
int rand()
{
return ((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
}
void srand(seed) u_int seed;
{
next = seed;
}
https://opensource.apple.com/source/Libc/Libc-166/stdlib.subproj/rand.c.auto.html
a
and b
Assume .
number%10; // between 0 and 9
number%(b-a+1); // between 0 and (b-a)
(number%(b-a+1))+a; // between a and b
rand_atob
Recall
(number%(b-a+1))+a // between a and b
Make int rand_atob(int a, int b)
which returns a random number from a
to b
using rand
function.
C++11 random
#include <random>
#include <iostream>
using namespace std;
int main()
{
std::random_device rd;//Make non-deterministic seed.
std::mt19937 gen(rd());// Mersenne Twister 19937bit generator.
std::uniform_int_distribution<int> dis(1, 10);// 1<= x <= 10
//std::uniform_real_distribution<double> dis(1.0, 10.0);
for (int n=0; n<10; ++n)
std::cout << dis(gen) << ' ';
std::cout << std::endl;
}
Example of screenshot.
10:21
11:23
12:25
13:17
14:14
sum:100
Sample:https://tinyurl.com/mr24hc8p https://tinyurl.com/2a9sc8x2
print
functionMake a print
which prints all elements of a given container of vector
, list
or deque
.
template <typename T> void print(const T & C);
Hint1: Use an iterator with begin()
and end()
member functions.
Hint2: Use ++
operator to move your iterator.
Sample: https://tinyurl.com/3whnkyc2
template <typename T> void randomWrite(vector<T> &);
template <typename T> void randomWrite(list<T> &);
template <typename T> void randomWrite(deque<T> &);
i
where .Note: For a random access, you need to reserve enough space first to avoid any out-of-range issues.
vector<long> myVector(100000);
list<long> myList(100000);
iterator
For a given iterator it
, ++it
ot it++
will move it
into the next.
For vector
and deque
, you can use it+n
to access the n
th successor of iterator it
.
vector<long> v= {0,1,2,3,4,5};
auto it=v.begin();
cout<<*(it+2);
How about list
?
next
and advance
-std::next(it, n)
returns the n
th successor of iterator it
.
https://en.cppreference.com/w/cpp/iterator/next
list<long> L={5,6,7,8,9};
auto it=L.begin(); // *it=5
auto it2=next(it,3); // *it2=8
-std::advance(it, n)
increments it
by n
elements.
https://en.cppreference.com/w/cpp/iterator/advance
advance(it, 2); // *it=7
vector
#define N 100000
template <typename T> void randomWrite(vector<T> &);
int main(int argc, const char * argv[]) {
srand(static_cast<int> (time(NULL)));
vector<long> myVector(N);
randomWrite(myVector);
return 0;
}
Sample: https://tinyurl.com/2p8t958n
list
#define N 100000
template <typename T> void randomWrite(vector<T> &);
int main(int argc, const char * argv[]) {
srand(static_cast<int> (time(NULL)));
list<long> myList(N);
randomWrite(myList);
return 0;
}
Sample: https://tinyurl.com/w6hmhnu8 https://tinyurl.com/mydsf45k
Concepts