code_junkie

What is the best way to create a sparse array in C++?

Nov 10th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <stdio.h>#include <stdlib.h>#include <map>class triple {public: int x; int y; int z; bool operator<(const triple &other) const { return (x < other.x && y < other.y && z < other.z); }};int main(int, char**){ std::map<triple,int> data; triple point; int i; for (i = 0; i < 10000000; ++i) { point.x = rand(); point.y = rand(); point.z = rand(); //printf("%d %d %d %dn", i, point.x, point.y, point.z); data[point] = i; } return 0;}
  2.  
  3. std::map data<string,int> data;char ix[100];sprintf(ix, "%d,%d", x, y); // 2 varsdata[ix] = i;sprintf(ix, "%d,%d,%d", x, y, z); // 3 varsdata[ix] = i;
  4.  
  5. typedef vector<size_t> index_t;
  6.  
  7. struct index_cmp_t : binary_function<index_t, index_t, bool> {
  8. bool operator ()(index_t const& a, index_t const& b) const {
  9. for (index_t::size_type i = 0; i < a.size(); ++i)
  10. if (a[i] != b[i])
  11. return a[i] < b[i];
  12. return false;
  13. }
  14. };
  15.  
  16. map<index_t, int, index_cmp_t> data;
  17. index_t i(dims);
  18. i[0] = 1;
  19. i[1] = 2;
  20. // … etc.
  21. data[i] = 42;
  22.  
  23. std::map data<string,int> data;char ix[100];sprintf(ix, "%d,%d", x, y); // 2 varsdata[ix] = i;sprintf(ix, "%d,%d,%d", x, y, z); // 3 varsdata[ix] = i;
Add Comment
Please, Sign In to add comment