Académique Documents
Professionnel Documents
Culture Documents
(TIC++V2:C6)
template<typename T> const T& min(const T& a, const T& b) { return (a < b) ? a : b; } int z = min<int>(i, j);
StringConv.h
int main() { int i = 1234; cout << "i == \"" << toString(i) << "\"" << endl; float x = 567.89; cout << "x == \"" << toString(x) << "\"" << endl; complex<float> c(1.0, 2.0); cout << "c == \"" << toString(c) << "\"" << endl; cout << endl; i = fromString<int>(string("1234")); cout << "i == " << i << endl; x = fromString<float>(string("567.89")); cout << "x == " << x << endl; c = fromString<complex<float> >(string("(1.0,2.0)")); cout << "c == " << c << endl;
StringConvTest.cpp
The output is what youd expect: i == "1234" x == "567.89" c == "(1,2)" i == 1234 x == 567.89 c == (1,2)
template<typename R, typename P> R implicit_cast(const P& p) { return p; } int main() { int i = 1; float x = implicit_cast<float>(i); int j = implicit_cast<int>(x); //! char* p = implicit_cast<char*>(i); } ///:~
Generic Algorithms
#include <algorithm> #include <cassert> #include <cstddef> // For size_t using namespace std; int main() { int a[] = { 10, 20, 30 }; const size_t SIZE = sizeof a / sizeof a[0]; int b[SIZE]; copy(a, a + SIZE, b); for(size_t i = 0; i < SIZE; ++i) assert(a[i] == b[i]); } ///:~
CopyInts.cpp
#include <algorithm> #include <cassert> #include <cstddef> #include <string> using namespace std; int main() { string a[] = {"read", "my", "lips"}; const size_t SIZE = sizeof a / sizeof a[0]; string b[SIZE]; copy(a, a + SIZE, b); // deep copy, new strings are creared assert(equal(a, a + SIZE, b)); } ///:~
CopyStrings.cpp
size_t: size for any types of varaibles. sizeof: operator to compute the size (number of elements) of an array. copy is defined as a generic algorithm. assert(): design by contract copy anything Predicate: remove_copy_ifremove_copy_if
template<typename T> void copy(T* begin, T* end, T* dest) { while(begin != end) *dest++ = *begin++;
#include <algorithm> #include <cassert> #include <cstddef> #include <vector> using namespace std; int main() { int a[] = { 10, 20, 30 }; const size_t SIZE = sizeof a / sizeof a[0]; vector<int> v1(a, a + SIZE); vector<int> v2(SIZE); copy(v1.begin(), v1.end(), v2.begin()); assert(equal(v1.begin(), v1.end(), v2.begin())); } ///:~
CopyVector.cpp
#include <algorithm> #include <cstddef> #include <iostream> using namespace std; // You supply this predicate bool gt15(int x) { return 15 < x; } int main() { int a[] = { 10, 20, 30 }; const size_t SIZE = sizeof a / sizeof a[0]; int b[SIZE]; int* endb = remove_copy_if(a, a+SIZE, b, gt15); int* beginb = b; while(beginb != endb) cout << *beginb++ << endl; // Prints 10 only } ///:~
CopyInts2.cpp