Google
 

4/13/07

sort two vectors.

cvcl-20060527/src/include/cvclutil.h - Google Code Search
sort one vector based on the other.

template
std::pair strPair(const std::string& f, const T& t) {
return std::pair(f, t);
}

typedef std::pair StrPair;

//! Sort two vectors based on the first vector
template
void sort2(std::vector& keys, std::vector& vals) {
DebugAssert(keys.size()==vals.size(), "sort2()");
// Create std::vector of pairs std::vector > pairs;
for(size_t i=0, iend=keys.size(); i pairs.push_back(strPair(keys[i], vals[i]));
// Sort pairs
StrPairLess comp;
sort(pairs.begin(), pairs.end(), comp);
DebugAssert(pairs.size() == keys.size(), "sort2()");
// Split the pairs back into the original vectors
for(size_t i=0, iend=pairs.size(); i keys[i] = pairs[i].first;
vals[i] = pairs[i].second;
}
}

}

No comments: