0
我想实现一个产生置换索引的程序。 Acctualy这是本练习: What is a permuted index?如何使C++中的置换索引
我已经写了一个函数,使一个旋转的弦弦的载体内,但我不知道如何保存多少度是每个字符串能够unrotate它稍后进行相同数量的旋转。 其实我有一个生成的旋转功能拆分句子转化成文字和功能:
#include <iostream>
#include <vector>
using namespace std;
vector<string> bookImplementation(const vector<string> &splitted) {
vector<string> result;
result = generateRotations(splitted);
// WHAT NEXT?
}
vector<string> split(const string &s) {
vector<string> ret;
string::size_type i = 0;
while (i != s.size()) {
while (i != s.size() && isspace(s[i]))
++i;
string::size_type j = i;
while (j != s.size() && !isspace(s[j]))
j++;
if (i != j) {
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
vector<string> generateRotations(const vector<string> &splitted) {
vector<string> result;
for (vector<string>::size_type i = 0; i != splitted.size(); ++i) {
string oneLine;
vector<string> temp(splitted);
//HOW TO SAVE NUMBER OF ROTATIONS (i)?
temp.insert(temp.begin(), temp.end() - i, temp.end());
temp.erase(temp.end() - i, temp.end());
for (vector<string>::size_type j = 0; j != temp.size(); ++j) {
oneLine += " ";
oneLine += temp[j];
}
result.push_back(oneLine);
}
return result;
}
int main() {
string phrase;
cout << "Please give me some phrase" << endl;
getline(cin, phrase);
vector <string> splitted = split(phrase);
vector<string> permuted = bookImplementation(splitted);
for (const auto i : permuted) {
cout << i << endl;
}
return 0;
}
如果有人告诉我,如果我做错什么事就好了。
是啊,这是我对这个任务唯一的想法和我sarted编码一样的,你写的,但书中有没有任何结构还没有提到,所以我想这可以用外部结构来完成。 – Mateusz