/////////////////////////////////////////////////////////////////////// //tr1RNG.cpp #include #include using namespace std::tr1; int main() { variate_generator > rngU(mt19937(123), uniform_real<>()); variate_generator > rngN(mt19937(123), normal_distribution<>(2, .5)); variate_generator > rngB(mt19937(123), binomial_distribution<>(3, .7)); variate_generator > rngP(mt19937(123), poisson_distribution<>(20)); variate_generator > rngG(mt19937(123), gamma_distribution<>(3)); for(int i = 0; i < 5; ++i) std::cout << rngU() << " " << rngN() << " " << rngB() << " " << rngP() << " " << 2*rngG() << std::endl; return 0; } /////////////////////////////////////////////////////////////////////// //tr1hashEx1.cpp #include #include #include using namespace std::tr1; using std::pair; int main() { unordered_map ht; ht.insert(pair (254,10)); ht.insert(pair (54,11)); ht.insert(pair (54,22)); for(unordered_map::iterator iter = ht.begin(); iter != ht.end(); iter++) std::cout << "(" << (*iter).first << "," << (*iter).second << ")" << " has count " << ht.count((*iter).first) << std::endl; std::cout << "Data value for key 54 = " << (ht.find(54))->second << std::endl; return 0; } /////////////////////////////////////////////////////////////////////// //tr1hashEx2.cpp #include #include #include using namespace std::tr1; using std::pair; int main() { unordered_multimap ht; ht.insert(pair (254,10)); ht.insert(pair (54,11)); ht.insert(pair (54,22)); for(unordered_multimap::iterator iter = ht.begin(); iter != ht.end(); iter++) std::cout << "(" << (*iter).first << "," << (*iter).second << ")" << " has count " << ht.count((*iter).first) << std::endl; pair::iterator, unordered_multimap::iterator> this_range = ht.equal_range(54); for(unordered_multimap::iterator iter = this_range.first; iter != this_range.second; iter++) { std::cout << iter->second << " "; } std::cout << std::endl; return 0; } /////////////////////////////////////////////////////////////////////// //tr1hashEx3.cpp #include #include #include #include using namespace std::tr1; int main() { unordered_map ht; std::cout << "Empty? " << std::boolalpha << ht.empty() << std::endl; ht.insert(std::pair ("example",10)); ht.insert(std::pair ("missing",9)); ht.insert(std::pair ("guess",15)); ht["value"]=16; std::cout << "Empty? " << ht.empty() << std::endl; std::cout << "Size: " << ht.size() << std::endl; for(unordered_map::iterator iter = ht.begin(); iter != ht.end(); iter++) std::cout << iter->first << " " << ht[iter->first] << " " << ht.count(iter->first) << std::endl; ht.erase("value"); std::cout << "value: " << ht.count("value") << std::endl; return 0; } /////////////////////////////////////////////////////////////////////// //tr1hashEx4.cpp #include #include #include #include using namespace std::tr1; using std::pair; struct Key : public pair { Key(std::string str, int id) : pair(str, id) {} bool operator == (const Key&a){ return this->first == a.first && this->second == a.second; } }; std::ostream& operator<<(std::ostream& out, const Key& key) { out << "(" << key.first << "," << key.second <<")"; return out; } namespace std { namespace tr1 { template<> struct hash : public unary_function { std::size_t operator ()(const Key& key) const { //functors for the string and int hash functions hash stringHash; hash intHash; size_t hashValue = (stringHash(key.first) + intHash(key.second)); return hashValue; } }; } } int main() { //3 buckets and hash as the hash function unordered_map ht(3, hash()); ht.max_load_factor(5);//average of 5 objects per bucket Key key1("s1", 2); Key key2("s2", 3); Key key3("s3", 4); Key key4("s4", 5); Key key5("s5", 6); Key key6("s6", 7); Key key7("s7", 8); Key key8("s8", 9); Key key9("s9", 10); ht[key1] = 1; ht[key2] = 2*ht[key1]; ht[key3] = 3*ht[key2]; ht[key4] = ht[key3]/4; ht[key5] = ht[key4]/5; ht[key6] = ht[key5]/6; ht[key7] = 7*ht[key6]; ht[key8] = 8*ht[key7]; ht[key9] = 9*ht[key8]; std::cout << "Size: " << ht.size() << ", Number of Buckets: " << ht.bucket_count() << std::endl; for(int i = 0; i < ht.bucket_count(); i++) std::cout << "Bucket " << i << " has " << ht.bucket_size(i) << " elements" << std::endl; std::cout << "Hash values" << std::endl; for(unordered_map::const_iterator iter = ht.begin(); iter != ht.end(); iter++) std::cout << iter->first << " was placed in bucket " << ht.bucket(iter->first) << std::endl; std::cout << "The elements in bucket 0 are" << std::endl; for(unordered_map::local_iterator iter = ht.begin(0); iter != ht.end(0); iter++) std::cout << iter->first << " "; std::cout << std::endl; return 0; } /////////////////////////////////////////////////////////////////////// //tupleEx.cpp #include #include #include #include "matrix.h" #include "vector.h" using std::tr1::tuple; using std::pair; using std::cout; using std::endl; using std::tr1::get; int main() { double** pMat = new double*[2]; double* pVec = new double[3]; for(int i = 0; i < 2; i++){ pMat[i] = new double[3]; for(int j = 0; j < 3; j++){ pMat[i][j] = (double)((i + 1)*(j + 1) + j); pVec[j] = (double)(j + 1); } } Matrix matrixObj(2, 3, pMat); Vector vectorObj(3, pVec); pair pairObj(matrixObj, vectorObj); tuple, Vector > tupleObj1(matrixObj, pairObj, vectorObj); get<0>(tupleObj1).printMatrix(); get<1>(tupleObj1).second.printVec(); Matrix newMat(2, 2, 1.); get<0>(tupleObj1) = newMat; get<0>(tupleObj1).printMatrix(); tuple, Vector> tupleObj2(tupleObj1); get<0>(tupleObj2).printMatrix(); return 0; } #tumplEx.mk tupleEx : tupleEx.o matrix.o vector.o g++ -o tupleEx tupleEx.o matrix.o vector.o matrix.o : matrix.cpp matrix.h vector.h g++ -c matrix.cpp vector.o : vector.cpp vector.h g++ -c vector.cpp tupleEx.o : tupleEx.cpp matrix.h vector.h g++ -c tupleEx.cpp ///////////////////////////////////////////////////////////////////////