我正在尝试执行加速 C++ 的示例 5.1,我应该在其中创建一个置换索引。所以我试图将每个旋转(当我cout
首先用语句测试时结果很好)放入自身的向量中。
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include "split.h"
using namespace std;
void rotate(vector<string> lineSplit){
// create vector of vectors, length is the amount
// of words. Each vector will contain a rotation
// std::vector<string> rotations(lineSplit.size(), "");
std::vector<string> rotations(lineSplit.size(), "");
for (int i=0; i<lineSplit.size(); i++){
cout << "New Rotation: " << endl;
for (int j=i; j<lineSplit.size(); j++){
rotations[i].push_back(lineSplit[j]);
cout << lineSplit[j] << endl;
}
// cout << "START IN HERE!" << endl;
for (int j=0; j<i; j++){
rotations[i].push_back(lineSplit[j]);
cout << lineSplit[j] << endl;
}
}
}
int main()
{
string line="The quick brown fox";
vector<string> lineSplit = split(line);
cout << "size of lineSplit is: " << lineSplit.size() << endl;
rotate(lineSplit);
}
所以旋转应该是一个向量向量,每个向量都包含一个旋转(根据注释语句)我想我可以用“rotations[i].push_back(lineSplit[j])”添加到每个单独的向量中编译器抱怨:
main.cpp: In function ‘void rotate(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’:
main.cpp:23: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
/usr/include/c++/4.2.1/bits/basic_string.h:869: note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
main.cpp:28: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
/usr/include/c++/4.2.1/bits/basic_string.h:869: note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
我是否错误地初始化了旋转?