std :: map :: begin () возвращает итератор с мусором

typedef unsigned long Count;
typedef float Weight;
typedef std::map<std::string, Count> StringToCountMap;
typedef std::map<std::string, Weight> StringToWeightMap;
typedef std::map<unsigned long, StringToCountMap> UnsignedToStringToCountMap;
typedef std::map<unsigned long, StringToWeightMap> UnsignedToStringToWeightMap;

typedef std::map<unsigned long, std::size_t> ClustersMap;class DefaultClusteringAlgorithm
{
public:
// minumum number of documents changing clusters for algorithm to end
static const unsigned DocumentChangeThreshold = 0;

DefaultClusteringAlgorithm(unsigned numClusters, const UnsignedToStringToWeightMap &documentVectors)
: numClusters_(numClusters)
, documentVectors_(documentVectors)
{
}

~DefaultClusteringAlgorithm() {}

const ClustersMap &DoClustering();

private:
void ChooseInitialCentroids();
unsigned ClusterOnCentroids();
void RecalculateCentroids();
float DocumentDotProduct(const StringToWeightMap &left, const StringToWeightMap &right);
float DocumentLength(const StringToWeightMap &document);

unsigned numClusters_;

// stores cluster_id => centroid
std::vector<StringToWeightMap> centroids_;

// maps question id => cluster id
ClustersMap clusters_;

// document vector
const UnsignedToStringToWeightMap &documentVectors_;
};

void DefaultClusteringAlgorithm::RecalculateCentroids()
{
std::vector<unsigned> newCentroidsSizes(centroids_.size());
std::vector<StringToWeightMap> newCentroids(centroids_.size());

ClustersMap::const_iterator clusterMapping = clusters_.begin();

for (; clusterMapping != clusters_.end(); ++clusterMapping)
{
std::size_t clusterId = clusterMapping->second;

++newCentroidsSizes[clusterId];
const StringToWeightMap &document = documentVectors_.at(clusterMapping->first);

StringToWeightMap::const_iterator termWeight = document.cbegin();

for (; termWeight != document.end(); ++termWeight);
{
newCentroids[clusterId][termWeight->first] += termWeight->second;
}
}

std::vector<unsigned>::iterator centroidSize = newCentroidsSizes.begin();

for (; centroidSize != newCentroidsSizes.end(); ++centroidSize)
{
std::size_t clusterId = centroidSize - newCentroidsSizes.begin();

StringToWeightMap::iterator centroidTermWeight = newCentroids[clusterId].begin();

for (; centroidTermWeight != newCentroids[clusterId].end(); ++centroidTermWeight)
{
centroidTermWeight->second /= *centroidSize;
}
}
}

часы отладчика

Проблема возникает при создании const_iterator termWeight:

StringToWeightMap::const_iterator termWeight = document.begin();

Как вы можете видеть на изображении выше, termWeight const_iterator содержит неверные данные. Тем не менее, документ const std :: map является совершенно корректным std :: map. Я не могу думать ни о какой причине, почему это происходит.

Недавно я узнал, что std :: map :: cbegin () существует. Должен ли я использовать этот метод вместо?

РЕДАКТИРОВАТЬ: Включено больше контекста

1

Решение

Хах! Я нашел ошибку! Глупая маленькая точка с запятой в конце моего цикла for!

2

Другие решения

Метод std :: map begin () может возвращать итератор, указывающий на конец карты, поскольку на карте вообще не может быть никаких элементов.

0