summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-09-06 22:32:49 +0100
committerPatrick Simianer <p@simianer.de>2014-09-06 22:32:49 +0100
commit626c2cd921e7442c20ac1be7be0b9ecb44d00c95 (patch)
treed9bb1d4c670ffc3e6094202dc2d5764040c4c1ff
parent01215a3f542df6692ef12b4e4b5fed551d065545 (diff)
cc examples
-rw-r--r--c,cc/base_derived_vector.cc35
-rw-r--r--c,cc/copy_struct.cc36
-rw-r--r--c,cc/delegate_iterator.cc32
-rw-r--r--c,cc/derived.cc28
-rw-r--r--c,cc/padding.cc32
-rw-r--r--c,cc/pairs_stringstream.cc22
-rw-r--r--c,cc/pointer_to_element.cc19
-rw-r--r--c,cc/reset_stringstream.cc25
-rw-r--r--c,cc/reuse_stringstream.cc23
-rw-r--r--c,cc/self_referencing_struct.cc19
-rw-r--r--c,cc/shared_ptr.cc31
-rw-r--r--c,cc/slicing.cc43
-rw-r--r--c,cc/struct_init.cc33
-rw-r--r--c,cc/vector_back.cc24
-rw-r--r--c,cc/vector_pointer.cc19
15 files changed, 421 insertions, 0 deletions
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 <iostream>
+#include <vector>
+
+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<Base> s;
+ Base a;
+ s.push_back(a);
+ Derived b;
+ s.push_back(b);
+ for (std::vector<Base>::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 <iostream>
+#include <vector>
+
+using namespace std;
+
+struct C {
+ vector<int> 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 <iostream>
+#include <vector>
+
+using namespace std;
+
+class A {
+ private:
+ typedef vector<int> 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 <iostream>
+#include <vector>
+
+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<Base*> 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 <iostream>
+#include <string>
+
+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 <iostream>
+#include <vector>
+#include <string>
+#include <sstream>
+
+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 <vector>
+#include <iostream>
+
+using namespace std;
+
+int
+main(void)
+{
+ vector<char> 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 <string>
+#include <iostream>
+#include <sstream>
+
+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 <sstream>
+#include <string>
+#include <iostream>
+
+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 <iostream>
+#include <vector>
+
+using namespace std;
+
+struct S {
+ int m;
+ vector<S> 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 <iostream>
+#include <memory>
+
+using namespace std;
+
+struct B {
+ B(int i) : m(i) {}
+ int m;
+};
+
+struct A {
+ shared_ptr<B> b;
+};
+
+int
+main(void)
+{
+ shared_ptr<B> 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 <iostream>
+#include <string>
+#include <vector>
+
+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<A> 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 <iostream>
+#include <utility>
+#include <vector>
+
+using namespace std;
+
+struct POD {
+ POD() { a = 2; }
+ POD(int x) { a = x; }
+ int a;
+};
+
+int
+main(void)
+{
+ vector<pair<char,char> > a;
+ pair<char,char> p = make_pair('a','b');
+ a.push_back(p);
+ vector<pair<char, char> > b(a);
+ cout << b[0].first << endl;
+ b[0].first = 'c';
+ cout << b[0].first << endl;
+
+ vector<POD> w;
+ POD x; x.a=3;
+ w.push_back(x);
+ POD y(x);
+ vector<POD> 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 <iostream>
+#include <vector>
+
+using namespace std;
+
+struct Foo {
+ int i;
+ char c;
+};
+
+int
+main(void)
+{
+ vector<Foo> foo;
+ foo.push_back(Foo());
+ foo.back().i = 3;
+ foo.back().c = 'c';
+
+ for (vector<Foo>::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 <vector>
+#include <iostream>
+
+using namespace std;
+
+int main(void)
+{
+ vector<char> 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;
+}
+