2012-11-13 01:20:50 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
2018-07-11 15:26:33 +02:00
|
|
|
#include <unordered_map>
|
2020-09-23 11:46:22 +02:00
|
|
|
#include <vector>
|
2012-11-13 01:20:50 +00:00
|
|
|
|
2018-07-11 15:26:33 +02:00
|
|
|
#include "ortools/base/hash.h"
|
2012-11-13 06:55:55 +00:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
2012-11-13 01:25:09 +00:00
|
|
|
struct Foo {
|
|
|
|
|
Foo() { std::cout << "Foo::Foo\n"; }
|
|
|
|
|
~Foo() { std::cout << "Foo::~Foo\n"; }
|
|
|
|
|
void bar() { std::cout << "Foo::bar\n"; }
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-23 11:46:22 +02:00
|
|
|
void f(const Foo& foo) { std::cout << "f(const Foo&)\n"; }
|
2012-11-13 01:20:50 +00:00
|
|
|
|
2012-11-13 01:25:09 +00:00
|
|
|
void test_unique() {
|
2012-11-13 16:06:51 +00:00
|
|
|
std::cout << "test_unique" << std::endl;
|
2012-11-13 01:25:09 +00:00
|
|
|
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
|
|
|
|
|
if (p1) p1->bar();
|
2012-11-13 01:20:50 +00:00
|
|
|
|
2012-11-13 01:25:09 +00:00
|
|
|
{
|
|
|
|
|
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
|
|
|
|
|
f(*p2);
|
2012-11-13 01:20:50 +00:00
|
|
|
|
2012-11-13 01:25:09 +00:00
|
|
|
p1 = std::move(p2); // ownership returns to p1
|
|
|
|
|
std::cout << "destroying p2...\n";
|
|
|
|
|
}
|
2012-11-13 01:20:50 +00:00
|
|
|
|
2012-11-13 01:25:09 +00:00
|
|
|
if (p1) p1->bar();
|
|
|
|
|
// Foo instance is destroyed when p1 goes out of scope
|
|
|
|
|
}
|
2012-11-13 01:20:50 +00:00
|
|
|
|
|
|
|
|
void test_auto() {
|
2012-11-13 16:06:51 +00:00
|
|
|
std::cout << "test_auto" << std::endl;
|
2012-11-13 01:20:50 +00:00
|
|
|
std::vector<int> numbers;
|
|
|
|
|
numbers.push_back(1);
|
|
|
|
|
numbers.push_back(2);
|
|
|
|
|
numbers.push_back(3);
|
|
|
|
|
numbers.push_back(4);
|
|
|
|
|
numbers.push_back(5);
|
|
|
|
|
numbers.push_back(6);
|
|
|
|
|
numbers.push_back(7);
|
2013-04-18 13:59:34 +00:00
|
|
|
for (int vec : numbers) {
|
2012-11-13 06:55:55 +00:00
|
|
|
std::cout << vec << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 11:46:22 +02:00
|
|
|
std::unordered_map<std::string, int> my_map;
|
2012-11-13 06:55:55 +00:00
|
|
|
my_map["toto"] = 2;
|
|
|
|
|
for (auto mm : my_map) {
|
|
|
|
|
std::cout << mm.first << " -> " << mm.second << std::endl;
|
2012-11-13 01:20:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-13 06:55:55 +00:00
|
|
|
void test_chevron() {
|
2012-11-13 16:06:51 +00:00
|
|
|
std::cout << "test_chevron" << std::endl;
|
2020-09-23 11:46:22 +02:00
|
|
|
std::vector<std::pair<int, int>> toto;
|
2012-11-13 06:55:55 +00:00
|
|
|
toto.push_back(std::make_pair(2, 4));
|
|
|
|
|
}
|
2015-04-16 12:00:07 +02:00
|
|
|
|
|
|
|
|
class A {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~A() {}
|
|
|
|
|
virtual int V() const { return 1; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class B : public A {
|
|
|
|
|
public:
|
|
|
|
|
~B() override {}
|
|
|
|
|
int V() const override { return 2; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void test_override() {
|
|
|
|
|
std::cout << "test_override" << std::endl;
|
|
|
|
|
B* b = new B();
|
|
|
|
|
if (b->V() != 2) {
|
|
|
|
|
std::cout << "Problem with override" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-13 06:55:55 +00:00
|
|
|
} // namespace operations_research
|
|
|
|
|
|
2012-11-13 01:20:50 +00:00
|
|
|
int main() {
|
2012-11-13 06:55:55 +00:00
|
|
|
operations_research::test_unique();
|
|
|
|
|
operations_research::test_auto();
|
|
|
|
|
operations_research::test_chevron();
|
2015-04-16 12:00:07 +02:00
|
|
|
operations_research::test_override();
|
2012-11-13 01:20:50 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|