mardi 12 février 2019

std::map Why it is not adding key , values in my code

I am trying to add key & value data to my class map member variable - but it does not adds the same -I tried map - insert, [] and emplace method but they are not adding data to map in my loop code - only the value I have inserted during my class construction is available in it - what I need to do to fix the issue - I was expecting that the show method will also print 7, 8,9, 9 -

#include <iostream>
#include <map> 
#include <vector>

  class A {
    public:
      A(std::initializer_list < uint32_t > d): data(d) {}
    std::vector < uint32_t > data;
    bool operator < (const A & rhs) const {
      size_t rhsSize = rhs.data.size();
      for (size_t i = 0; i < data.size(); i++) {
        if (i < rhsSize)
          return false;
        return true;
      }
    }
  };

class B {
  public:
    B(const std::map < A, uint32_t > & t): table(t) {}
  void Show() {
    for (std::map < A, uint32_t > ::iterator it = table.begin(); it != table.end(); ++it) {
      for (const auto & i: it->first.data)
        std::cout << i << "\n";
      std::cout << it->second << "\n";
    }
  }

  std::map < A, uint32_t > table;
};

int main() {
  std::map < A, uint32_t > tb = {
    {
      A {70, 8, 9,10}, 1234}
  };
  B b(tb);
  for (int i = 0; i < 2; i++) {
    b.Show();
    b.table.emplace(A {7, 8,9, 9}, 1234);
  }
  return 0;
}

Compile and run the code:

$ c++ -std=c++11 try78.cpp


$ ./a.exe
70
8
9
10
1234
70
8
9
10
1234

Aucun commentaire:

Enregistrer un commentaire