简体   繁体   English

填充向量的向量时出错:没有匹配的函数来调用std :: basic_string :: push_back

[英]Error populating a vector of vectors: no matching function to call for std::basic_string::push_back

I'm trying to do Accelerated C++'s example 5.1 in which I am supposed to create a permuted index. 我正在尝试做Accelerated C ++的示例5.1,其中应该创建一个置换索引。 So I am trying to put each rotation (which come out fine as I tested with the cout statements first) into a vector of itself. 因此,我尝试将每个旋转(在我首先使用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);

}

So rotations should be a vector of vectors, each one containing a rotation (as per the comment statements) I thought I would be able to add to each individual vector with "rotations[i].push_back(lineSplit[j])" but the compiler is complaining: 因此,旋转应该是向量的向量,每个向量都包含一个旋转(根据注释语句),我认为我可以使用“ 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>]

Did I initialize rotations incorrectly? 我是否初始化旋转错误?

 rotations[i].push_back(lineSplit[j]);

rotations is vector of string, so rotations[i] is string. rotations是字符串的向量,因此rotations[i]是字符串。 lineSplit is vector of string, so lineString[i] is string. lineSplit是字符串的向量,因此lineString[i]是字符串。 You can push_back only chars in string, not string in string. 您只能push_back字符串中的字符,而不能字符串中的字符。 Look like, that rotations should have type vector<vector<string> > . 看起来, rotations应具有类型vector<vector<string> >

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有匹配函数来调用&#39;std :: vector <std::__cxx11::basic_string<char> &gt; ::的push_back(INT&)” - error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’ 错误:没有匹配的函数调用&#39;std :: vector <std::vector<int> &gt; ::的push_back(标准::矢量 <std::__cxx11::basic_string<char> &gt;&)” - error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’ 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back 错误:没有匹配的 function 调用 'std::vector<float> ::push_back(std::vector<float> &amp;) 常量'</float></float> - error: no matching function for call to 'std::vector<float>::push_back(std::vector<float>&) const' std :: basic_string中的push_back()与序列容器 - push_back() in std::basic_string vs sequence containers 没有用于调用 &#39;std::vector 的匹配函数<node*> ::push_back(节点*&amp;)&#39; - no matching function for call to ‘std::vector<node*>::push_back(Node*&)’ 没有匹配的 function 调用 'std::vector::push_back(std::string&amp;)' - No matching function for call to ‘std::vector::push_back(std::string&)’ 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 错误:没有匹配的函数来调用“std::vector”<x> ::push_back(y&amp;) 在 C++ - error: no matching function for call to ‘std::vector<x>::push_back(y&) in C++ 错误:没有用于调用 vector::push_back 的匹配函数 - error: no matching function for call to vector::push_back
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM