我正在尝试使用Accelerated 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>]
我是否错误地初始化了旋转?
答案 0 :(得分:2)
rotations[i].push_back(lineSplit[j]);
rotations
是字符串的向量,因此rotations[i]
是字符串。 lineSplit
是字符串的向量,因此lineString[i]
是字符串。
您push_back
只能在字符串中使用字符,而不能在字符串中使用字符串。
看起来,rotations
应该有vector<vector<string> >
类型。