我正在使用 std::map(( 制作一个 2D 表格,以计算一个数字转换为另一个数字的次数。 我遇到了两个问题。 首先,我的第一个转换没有显示 (1->2(。 其次,我所有的转换都只显示一次(2->3 和 3->1 都发生了两次(。
我明白为什么过渡只发生一次。 迭代器看不到 currentVal 并转到 else,在那里它添加值然后退出。 不过,我不确定如何解决此问题。 任何帮助不胜感激!
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
//import midi notes
vector <int> midiFile = {1, 2, 3, 1, 20, 5, 2, 3, 1};
//create a 2d hashmap for matrix
map <string, map <string, int> > mCounts;
//strings for the previous value and current value
string prevVal = "";
string currentVal = "";
void addNumbers(vector <int> midiFile) {
for (int i = 0; i < midiFile.size(); i++) {
currentVal = to_string(midiFile[i]);
if(prevVal == "") {
prevVal = currentVal; //first value
} else {
//playCounts is temporary map to store counts of current val in relation to previous val
map <string, int> playCounts;
map <string, int> ::iterator iterator;
iterator = playCounts.find(currentVal);
//if mCounts doesn't contain the value yet, create a new hashmap
if(iterator != playCounts.end()){
int counter = iterator -> second;
mCounts[prevVal] [currentVal] = counter + 1;
} else {
playCounts.insert(pair <string, int>(currentVal, 1));
mCounts [prevVal] = playCounts;
}
prevVal = currentVal;
}
//find values already in map
map <string, map <string, int> > ::iterator it;
it = mCounts.find(prevVal);
if (it != mCounts.end()) {
//if value is found, do nothing
} else {
mCounts.insert(pair <string, map <string, int>>(prevVal, map <string, int>()));
}
}
}
您正在处理 128
int transition[128][128];
当然,您希望键入定义并通过引用传递它。不仅您的所有操作都更容易、更透明,而且使用转移矩阵可以进行任何其他方式都无法实现的分析:平衡状态的特征向量等。
对于转换稀疏且负担不起密集矩阵的较大问题,请使用实际的稀疏矩阵类,这本质上是您自己尝试滚动的内容。
typedef int transitionMatrix[128][128];
void addNumbers(const vector <int> &midiFile, transitionMatrix &mCounts) {
for (int i = 0; i < midiFile.size()-1; i++) {
int prev = midiFile[i];
int curr = midiFile[i+1];
mCounts[prev][curr]++;
}
}
尝试以下方法,其中构成转换的两个整数组合成一个形式的字符串 "1->2"
,然后作为计数映射中的键。这样代码变得更加简洁。此外,我消除了全局变量并使它们成为局部变量或参数:
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
using std::vector;
using std::map;
using std::string;
void addNumbers(const vector <int> &midiFile, map <string, int> &mCounts) {
for (int i = 0; i < midiFile.size()-1; i++) {
int prev = midiFile[i];
int curr = midiFile[i+1];
std::stringstream ss;
ss << prev << "->" << curr;
mCounts[ss.str()]++;
}
}
int main(int argc, char* argv[])
{
vector <int> midiFile = {1, 2, 3, 1, 20, 5, 2, 3, 1};
map <string, int> mCounts;
addNumbers(midiFile, mCounts);
for (auto const& x : mCounts)
{
std::cout << x.first // transition
<< ':'
<< x.second // count
<< std::endl ;
}
return 0;
}
输出:
1->2:1
1->20:1
2->3:2
20->5:1
3->1:2
5->2:1
以下是不使用嵌套映射且不将注释转换为字符串(但假设注释为非负数(的解决方案:
// This snippet uses c++11 syntax
#include <map>
// Code in this example assumes that valid notes are nonnegative
struct transition {
int from;
int to;
};
// Comparison operator required to make transition usable as a
// key in std::map
bool operator< (const transition& l, const transition& r) {
return l.from < r.from || (l.from == r.from && l.to < r.to);
}
// Range of all transitions with respective counter
// starting from a particular note
std::pair<std::map<transition, int>::const_iterator,
std::map<transition, int>::const_iterator>
transitions_from(int from_note,
const std::map<transition, int>& transition_counters) {
return std::make_pair(transition_counters.lower_bound(transition{from_note, -1}),
transition_counters.upper_bound(transition{from_note + 1, -1}));
}
int counter_for(transition t, const std::map<transition, int>& transition_counters) {
const auto it = transition_counters.find(t);
if (it != transition_counters.end()) {
return it->second;
} else {
return 0;
}
}
使用示例:
#include <iostream>
#include <vector>
int main() {
std::vector<int> notes = {1, 2, 3, 1, 20, 5, 2, 3, 1};
std::map<transition, int> transition_counters;
int previous_note = -1;
for (int note: notes) {
if (previous_note != -1) {
transition t{previous_note, note};
transition_counters[t] += 1;
}
previous_note = note;
}
std::cout << "all encountered transitions:n";
for (const auto& entry: transition_counters) {
std::cout << '(' << entry.first.from << " -> " << entry.first.to << "): " << entry.second << 'n';
}
std::cout << "transitions from 1:n";
const auto transitions_from_1 = transitions_from(1, transition_counters);
for (auto it = transitions_from_1.first; it != transitions_from_1.second; ++it) {
std::cout << '(' << it->first.from << " -> " << it->first.to << "): " << it->second << 'n';
}
std::cout << "counters for individual transitions:n";
std::cout << "(1 -> 2): " << counter_for(transition{1, 2}, transition_counters) << 'n';
std::cout << "(2 -> 1): " << counter_for(transition{2, 1}, transition_counters) << 'n';
}