Skip to content
Snippets Groups Projects
Commit 6f504f3b authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Weitere Beispiele 6.4.2023: Iteratoren, Templates

parent ee0d6d73
Branches
No related tags found
No related merge requests found
Showing
with 475 additions and 0 deletions
Hallo, Welt!
#include <iostream>
int main ()
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p != prime + 5; p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
int main ()
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p <= prime + 4; p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main ()
{
std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (auto p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (auto p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (auto p : prime)
std::cout << *p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (auto p : prime)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (auto &p : prime)
std::cout << p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (auto &p : prime)
std::cout << p << std::endl;
return 0;
}
#include <iostream>
#include <list>
int main ()
{
std::list <int> prime = { { 2, 3, 5, 7, 11 } };
for (auto &p : prime)
std::cout << p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (auto &p : prime)
std::cout << p << std::endl;
return 0;
}
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void print (int x)
{
cout << x << endl;
}
void print (string x)
{
cout << x << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
return 0;
}
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
template <typename t>
void print (t x)
{
cout << x << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
template <typename t>
void print (t x)
{
cout << x << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class bla
{
int content;
};
template <typename t>
void print (t x)
{
cout << x << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
bla blubb;
print (blubb);
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class bla
{
public:
int content;
bla (int x):
content (x)
{
}
};
template <typename t>
void print (t x)
{
cout << x << endl;
}
template <>
void print (bla x)
{
cout << x.content << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
bla blubb (137);
print (blubb);
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class bla
{
public:
int content;
bla (int x):
content (x)
{
}
};
template <typename t>
void print (t x)
{
cout << x << endl;
}
template <>
void print (int x)
{
cout << "Die Antwort lautet: " << x << endl;
}
template <>
void print (bla x)
{
cout << x.content << endl;
}
int main ()
{
print (42);
print ("Hello, world!");
bla blubb (137);
print (blubb);
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
class bla
{
public:
int content;
bla (int x):
content (x)
{
}
};
template <typename t>
void print (t x)
{
cout << x << endl;
}
template <>
void print (int x)
{
cout << "Die Antwort lautet: " << x << endl;
}
template <>
void print (bla x)
{
cout << x << endl; // Fehler wird erkannt, obwohl nicht instantiiert.
} // Dies ist nicht immer der Fall.
int main ()
{
print (42);
print ("Hello, world!");
bla blubb (137);
// print (blubb);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
template <typename argtype>
class list
{
struct node
{
argtype content;
node *next;
};
node *root;
public:
list ();
void insert (argtype x);
void output ();
};
template <typename argtype>
list <argtype>::list ()
{
root = NULL;
}
template <typename argtype>
void list <argtype>::insert (argtype x)
{
node *n = new node;
n->content = x;
n->next = root;
root = n;
}
template <typename argtype>
void list <argtype>::output ()
{
for (list *p = root; p; p = p->next)
cout << p->content << endl;
}
int main ()
{
list <string> s;
s.insert ("one");
s.insert ("two");
s.insert ("three");
list <int> i;
i.insert (1);
i.insert (2);
i.insert (3);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
template <typename argtype>
class list
{
struct node
{
argtype content;
node *next;
};
node *root;
public:
list ();
void insert (argtype x);
void output ();
};
template <typename argtype>
list <argtype>::list ()
{
root = NULL;
}
template <typename argtype>
void list <argtype>::insert (argtype x)
{
node *n = new node;
n->content = x;
n->next = root;
root = n;
}
template <typename argtype>
void list <argtype>::output ()
{
for (list *p = root; p; p = p->next)
cout << p->content << endl;
}
int main ()
{
list <string> s;
s.insert ("one");
s.insert ("two");
s.insert ("three");
s.output ();
list <int> i;
i.insert (1);
i.insert (2);
i.insert (3);
i.output ();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
template <typename argtype>
class list
{
struct node
{
argtype content;
node *next;
};
node *root;
public:
list ();
void insert (argtype x);
void output ();
};
template <typename argtype>
list <argtype>::list ()
{
root = NULL;
}
template <typename argtype>
void list <argtype>::insert (argtype x)
{
node *n = new node;
n->content = x;
n->next = root;
root = n;
}
template <typename argtype>
void list <argtype>::output ()
{
for (node *p = root; p; p = p->next)
cout << p->content << endl;
}
int main ()
{
list <string> s;
s.insert ("one");
s.insert ("two");
s.insert ("three");
s.output ();
list <int> i;
i.insert (1);
i.insert (2);
i.insert (3);
i.output ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment