From 626c2cd921e7442c20ac1be7be0b9ecb44d00c95 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 6 Sep 2014 22:32:49 +0100 Subject: cc examples --- c,cc/base_derived_vector.cc | 35 +++++++++++++++++++++++++++++++++ c,cc/copy_struct.cc | 36 ++++++++++++++++++++++++++++++++++ c,cc/delegate_iterator.cc | 32 ++++++++++++++++++++++++++++++ c,cc/derived.cc | 28 +++++++++++++++++++++++++++ c,cc/padding.cc | 32 ++++++++++++++++++++++++++++++ c,cc/pairs_stringstream.cc | 22 +++++++++++++++++++++ c,cc/pointer_to_element.cc | 19 ++++++++++++++++++ c,cc/reset_stringstream.cc | 25 ++++++++++++++++++++++++ c,cc/reuse_stringstream.cc | 23 ++++++++++++++++++++++ c,cc/self_referencing_struct.cc | 19 ++++++++++++++++++ c,cc/shared_ptr.cc | 31 +++++++++++++++++++++++++++++ c,cc/slicing.cc | 43 +++++++++++++++++++++++++++++++++++++++++ c,cc/struct_init.cc | 33 +++++++++++++++++++++++++++++++ c,cc/vector_back.cc | 24 +++++++++++++++++++++++ c,cc/vector_pointer.cc | 19 ++++++++++++++++++ 15 files changed, 421 insertions(+) create mode 100644 c,cc/base_derived_vector.cc create mode 100644 c,cc/copy_struct.cc create mode 100644 c,cc/delegate_iterator.cc create mode 100644 c,cc/derived.cc create mode 100644 c,cc/padding.cc create mode 100644 c,cc/pairs_stringstream.cc create mode 100644 c,cc/pointer_to_element.cc create mode 100644 c,cc/reset_stringstream.cc create mode 100644 c,cc/reuse_stringstream.cc create mode 100644 c,cc/self_referencing_struct.cc create mode 100644 c,cc/shared_ptr.cc create mode 100644 c,cc/slicing.cc create mode 100644 c,cc/struct_init.cc create mode 100644 c,cc/vector_back.cc create mode 100644 c,cc/vector_pointer.cc diff --git a/c,cc/base_derived_vector.cc b/c,cc/base_derived_vector.cc new file mode 100644 index 0000000..e20a43d --- /dev/null +++ b/c,cc/base_derived_vector.cc @@ -0,0 +1,35 @@ +#include +#include + +class Base { +public: + virtual void + f() + { + std::cout << "Base\n"; + } +}; + +class Derived: public Base { +public: + virtual void + f() + { + std::cout << "Derived\n"; + } +}; + +int +main() +{ + std::vector s; + Base a; + s.push_back(a); + Derived b; + s.push_back(b); + for (std::vector::iterator it = s.begin(); it != s.end(); ++it) + it->f(); + + return 0; +} + diff --git a/c,cc/copy_struct.cc b/c,cc/copy_struct.cc new file mode 100644 index 0000000..cb23b09 --- /dev/null +++ b/c,cc/copy_struct.cc @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +struct C { + vector i; +}; + +struct B { + C c; +}; + +struct A { + B b; +}; + +int +main(void) +{ + A a; + B b; + C c; + + c.i.push_back(23); + b.c = c; + a.b = b; + + cout << a.b.c.i[0] << endl; + + A x(a); + cout << x.b.c.i[0] << endl; + + return 0; +} + diff --git a/c,cc/delegate_iterator.cc b/c,cc/delegate_iterator.cc new file mode 100644 index 0000000..c4e272e --- /dev/null +++ b/c,cc/delegate_iterator.cc @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +class A { + private: + typedef vector Data; + Data data_; + public: + typedef Data::iterator iterator; + typedef const Data::iterator const_iterator; + iterator begin() { return data_.begin(); } + iterator end() { return data_.end(); } + + void add(int i) { + data_.push_back(i); + } +}; + +int +main(void) +{ + A a; + a.add(23); + for (A::iterator it = a.begin(); it != a.end(); it++) { + cout << *it << endl; + } + + return 0; +} + diff --git a/c,cc/derived.cc b/c,cc/derived.cc new file mode 100644 index 0000000..809378a --- /dev/null +++ b/c,cc/derived.cc @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +struct Base { + virtual int + index() { return -1; }; +}; + +struct D1 : public Base { + int idx = 1; + + virtual int + index() { return idx; } +}; + +int +main(void) +{ + vector v; + D1* d = new D1; + v.push_back(d); + cout << v.back()->index() << endl; + + return 0; +} + diff --git a/c,cc/padding.cc b/c,cc/padding.cc new file mode 100644 index 0000000..7ac9be7 --- /dev/null +++ b/c,cc/padding.cc @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +struct foo { + int a; + string b; + string b1; + int c; + string b2; + string b3; +}; + +struct bar { + int a; + int c; + string b; + string b1; + string b2; + string b3; +}; + +int +main(void) +{ + cout << sizeof(foo) << endl; + cout << sizeof(bar) << endl; + + return 0; +} + diff --git a/c,cc/pairs_stringstream.cc b/c,cc/pairs_stringstream.cc new file mode 100644 index 0000000..300d55e --- /dev/null +++ b/c,cc/pairs_stringstream.cc @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +using namespace std; + +int +main(void) +{ + istringstream ss("ufllffrrf"); + ss.seekg(1); + while (ss.good()) { + char prev, cur; + ss >> prev; + ss >> cur; + cout << prev << " " << cur << endl; + } + + return 0; +} + diff --git a/c,cc/pointer_to_element.cc b/c,cc/pointer_to_element.cc new file mode 100644 index 0000000..5a11919 --- /dev/null +++ b/c,cc/pointer_to_element.cc @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int +main(void) +{ + vector bla; + bla.reserve(2); + bla.push_back('a'); + char* p = &bla.at(0); + cout << *p << endl; + bla.push_back('b'); + cout << *p << endl; + + return 0; +} + diff --git a/c,cc/reset_stringstream.cc b/c,cc/reset_stringstream.cc new file mode 100644 index 0000000..474f014 --- /dev/null +++ b/c,cc/reset_stringstream.cc @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int +main(void) +{ + string s("X,1"); + string t("1"); + int i; + istringstream ss(s); + if (ss >> i) { + cout << i << endl; + } else { + ss.clear(); + string buf; + while(ss.good() && getline(ss,buf,',')) + cout << buf << endl; + } + + return 0; +} + diff --git a/c,cc/reuse_stringstream.cc b/c,cc/reuse_stringstream.cc new file mode 100644 index 0000000..f214544 --- /dev/null +++ b/c,cc/reuse_stringstream.cc @@ -0,0 +1,23 @@ +#include +#include +#include + +using namespace std; + +int +main(void) +{ + ostringstream ss; + for (int i = 0; i < 5; i++) { + ss.str(string()); + ss << "asdf"; + ss << i; + ss << i; + ss << i; + ss << i; + cout << ss.str() << endl; + } + + return 0; +} + diff --git a/c,cc/self_referencing_struct.cc b/c,cc/self_referencing_struct.cc new file mode 100644 index 0000000..c084164 --- /dev/null +++ b/c,cc/self_referencing_struct.cc @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +struct S { + int m; + vector v; +}; + +int +main(void) +{ + S a; + S b; + a.v.push_back(b); + return 0; +} + diff --git a/c,cc/shared_ptr.cc b/c,cc/shared_ptr.cc new file mode 100644 index 0000000..4fb158e --- /dev/null +++ b/c,cc/shared_ptr.cc @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +struct B { + B(int i) : m(i) {} + int m; +}; + +struct A { + shared_ptr b; +}; + +int +main(void) +{ + shared_ptr x(new B(42)); + A* a = new A; + A* c = new A; + a->b = x; + c->b = x; + cout << x.use_count() << endl; + delete a; + cout << x.use_count() << endl; + delete c; + cout << x.use_count() << endl; + + return 0; +} + diff --git a/c,cc/slicing.cc b/c,cc/slicing.cc new file mode 100644 index 0000000..c89d99d --- /dev/null +++ b/c,cc/slicing.cc @@ -0,0 +1,43 @@ +#include +#include +#include + +using namespace std; + +struct A { + public: + virtual string s() { return "a"; } +}; + +class B : public A { + public: + string s() { return "b"; } +}; + +B +make_b() +{ + B b; + return b; +} + +A +make_a() +{ + return make_b(); +} + +int +main(void) +{ + vector x; + B b = make_b(); + x.push_back(b); + cout << x[0].s() << endl; + B c; + x.push_back(c); + cout << x[1].s() << endl; + + return 0; +} + diff --git a/c,cc/struct_init.cc b/c,cc/struct_init.cc new file mode 100644 index 0000000..e54724a --- /dev/null +++ b/c,cc/struct_init.cc @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +struct POD { + POD() { a = 2; } + POD(int x) { a = x; } + int a; +}; + +int +main(void) +{ + vector > a; + pair p = make_pair('a','b'); + a.push_back(p); + vector > b(a); + cout << b[0].first << endl; + b[0].first = 'c'; + cout << b[0].first << endl; + + vector w; + POD x; x.a=3; + w.push_back(x); + POD y(x); + vector q(w); + cout << q[0].a << endl; + + return 0; +} + diff --git a/c,cc/vector_back.cc b/c,cc/vector_back.cc new file mode 100644 index 0000000..cbc82f5 --- /dev/null +++ b/c,cc/vector_back.cc @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +struct Foo { + int i; + char c; +}; + +int +main(void) +{ + vector foo; + foo.push_back(Foo()); + foo.back().i = 3; + foo.back().c = 'c'; + + for (vector::iterator it = foo.begin(); it != foo.end(); it++) + cout << it->i << " " << it->c << endl; + + return 0; +} + diff --git a/c,cc/vector_pointer.cc b/c,cc/vector_pointer.cc new file mode 100644 index 0000000..70251e6 --- /dev/null +++ b/c,cc/vector_pointer.cc @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + +int main(void) +{ + vector x; + x.push_back('a'); + x.push_back('b'); + x.push_back('c'); + char* p = &(x[1]); + cout << *p << endl; + x[1] = 'x'; + cout << *p << endl; + + return 0; +} + -- cgit v1.2.3