Thank you for all the responses, I had to take a couple of days away from it, so just checking now.
As the courses get further on on pluralsight it seems that the lecturer of the classes seem to be less responsive, as people have seem to have commented on the code already without response.
The code was meant to teach move semantics. There was more to the code, but didnt think it was too important when I was just asking about value_type. But im having an issue now understanding the move semantics, not sure if i am best asking a new question, or just posting again since the code is the same, so i will just do that. The full code is below.
What I am wondering is, in the line v.insert(it, res); the copy constructor is called, is that making a copy of res? Is that making a temp object of res? Then there is a move assignment that happens with that value, which is the temp value being moved from the temp object into the vector.
Also, when the first element 'starting entry' needs to move, the move constructor is called - i was told that this is called when an object is created from a object that is about to go away. So is this called because nothing exists in the position that starting entry is moving to, and starting entry is about to be removed from its first position?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include "Resource.h"
#include <iostream>
#include <iterator>
#include <chrono>
using std::chrono::system_clock;
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::seconds;
using std::chrono::milliseconds;
#include <string>
using std::string;
#include <vector>
#include <list>
using std::vector;
using std::list;
#include <algorithm>
template <typename Func>
long long TimeFunc(Func f)
{
auto begin = steady_clock::now();
f();
auto end = steady_clock::now();
return duration_cast<milliseconds>(end - begin).count();
}
template <typename T>
void exercise(int size, string commonname)
{
T v;
v.reserve(50);
//T::template value_type res1("starting entry");
typename T::value_type res1("starting entry");
v.push_back(res1);
for (int i = 0; i < size; i++)
{
std::cout << "string container\n";
int r = (int)rand();
string s;
int ss = r % 100;
s.reserve(ss);
std::generate_n(std::back_inserter(s), ss, [ss]() {return (char)('!' + ss / 2); });
T::template value_type res(s);
bool inserted = false;
for (auto it = begin(v); it != end(v); it++)
{
if (*it > res)
{
v.insert(it, res);
inserted = true;
break;
}
}
if (!inserted)
v.push_back(res);
}
auto it = max_element(begin(v), end(v));
T::template value_type maxV = *it;
it = min_element(begin(v), end(v));
T::template value_type minV = *it;
bool sorted = is_sorted(begin(v), end(v));
}
int main()
{
const int size = 1000;
auto vectormilliseconds = TimeFunc([&]() {exercise<vector<Resource>>(size, "vector"); });
//auto listmilliseconds = TimeFunc([&]() {exercise<list<Resource>>(size, "list"); });
}
|
resource.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#pragma once
#include <string>
#include <fstream>
#include <iostream>
class Resource
{
private:
std::string name;
public:
Resource(std::string n);
Resource(const Resource& r);
Resource& operator=(const Resource& r);
Resource(Resource&& r);
Resource& operator=(Resource&& r);
~Resource(void);
bool operator>(const Resource& r) { return name > r.name; }
bool operator<(const Resource& r) { return name < r.name; }
bool operator==(const Resource& r) { return name == r.name; }
friend std::ostream& operator<<(std::ostream& s, const Resource& obj);
std::string GetName() const { return name; }
};
|
resource.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "Resource.h"
#include <iostream>
using std::cout;
using std::string;
Resource::Resource(string n) :name(n)
{
cout << "constructing " << name << '\n';
}
Resource::Resource(const Resource& r) : name(r.name)
{
cout << "copy constructing " << name << '\n';
}
Resource& Resource::operator=(const Resource& r)
{
//if class contained true Resources, clean up existing ones
//before setting new values
name = r.GetName();
cout << "copy assigning " << name << '\n';
return *this;
}
Resource::Resource(Resource&& r) :name(std::move(r.name))
{
cout << " move construuctor " << name << "\n";
}
Resource& Resource::operator=(Resource&& r)
{
if (this != &r)
{
name = std::move(r.name);
r.name.clear();
cout << "move assigning " << name << '\n';
}
return *this;
}
Resource::~Resource(void)
{
// cout << "destructing " << name << '\n';
}
std::ostream& operator<<(std::ostream& s, const Resource& obj)
{
s << obj.name << " ";
return s;
}
|