简体   繁体   English

没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..>

[英]no matching function for call to 'std::vector<std.. '

The question I am solving states: I have to take T test cases.我正在解决的问题是:我必须采用T测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: string at even position {double space} string at odd position (example: input - StackOverflow, output - Sakvrlw tcOefo ). For each test case I have to take string as an input, then I need to arrange the input string as: string at even position {double space} string at odd position (example: input - StackOverflow, output - Sakvrlw tcOefo ). I've written the following code where I am taking input for all test cases and storing it in a vector.我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 Then I am assigning the elements of vector to another declared string s.然后我将 vector 的元素分配给另一个声明的 string s。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
     int T,i;
     cout << "Enter no. of test cases: ";
     cin >> T;

     vector<string> v;
     vector<string> odd;
     vector<string> even;
     string str;

     for(int i=0; i<T; i++){
        cin >> str;
        v.push_back(str);
     }


     string s;

     for(i=0; i<v.size(); i++){
        s = v.at(i);

        for(i=0; i<s.size(); i++){
            if(i==0){
                even.push_back(s[i]);  //*This is where I am getting error*.
            }else if(i==1){
                odd.push_back(s[i]);
            }else{
                if(i%2==0){
                    even.push_back(s[i]);
                }else{
                    odd.push_back(s[i]);
                }
            }
        }

        for(i=0; i<even.size(); i++){
            cout << even.at(i);
        }

        cout << "  ";

        for(i=0; i<odd.size(); i++){
            cout << odd.at(i);
        }

        cout << endl;

        even.clear();
        odd.clear();
        s.clear();
     }


     return 0;
}


While compiling the above code I'm getting "no matching error for call std::vector..." .在编译上面的代码时,我得到"no matching error for call std::vector..." What exactly am I doing wrong?我到底做错了什么?

I am getting the following error when compiling your code:编译代码时出现以下错误:

main.cpp:34:36: error: no matching function for call to 'std::vector >::push_back(char&)' . main.cpp:34:36: error: no matching function for call to 'std::vector >::push_back(char&)'

This happens because even is a vector<string> , and s[i] is a char .发生这种情况是因为evenvector<string> ,而s[i]char You are trying to insert a char into a vector of strings, and that is not possible, as they are different types.您正在尝试将 char 插入到字符串向量中,这是不可能的,因为它们是不同的类型。

If I understood your problem properly, even and odd must both be either vector<char> or string , not vector<string> .如果我正确理解了您的问题,则evenodd都必须是vector<char>string而不是vector<string>

Change the declarations to:将声明更改为:

string odd;
string even;

This also allows you to replace the printing:这也允许您替换打印:

for(i=0; i<even.size(); i++) {
    cout << even.at(i);
}

With:和:

cout << even;

Welcome to Stack Overflow @Manish Jha,欢迎来到 Stack Overflow @Manish Jha,

So s[i] is a char所以s[i]是一个char

s is a std::string or an array of char s sstd::stringchar数组

The method must be push_back(std::string&) not push_back(std::char&)该方法必须是push_back(std::string&)而不是push_back(std::char&)

The class template method std::vector::push_back(T&) is kind of "specialized" when you initialized your even/odd vector so, either append a std::string to std::vector<std::string> or a char to std::vector<char>当您初始化偶数/奇数向量时,class 模板方法std::vector::push_back(T&)是一种“专用”,因此 append 将std::string转换为std::vector<std::string>charstd::vector<char>

Otherwise, there is no maching for call the method std::vector<T>::push_back(T&)否则,没有调用方法std::vector<T>::push_back(T&)

Hope i answered correctly to your waitings, if there are any errors, please notify me.希望我能正确回答您的等待,如果有任何错误,请通知我。

暂无
暂无

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

相关问题 没有匹配的函数来调用&#39;merge(std :: vector <int> &,std :: vector <int> &) - No matching function for call to 'merge(std::vector<int>&, std::vector<int>&) 传递std向量作为参考:没有匹配的函数来调用 - Passing std vector as reference: no matching function to call 没有匹配的函数来调用 std::vector<float> - No matching function to call to std::vector<float> 错误:没有匹配的 function 调用 'recherche(std::vector &gt;&amp;, std::vector &gt;::iterator, std::vector &gt;::iterator, const char [10])' - error: no matching function for call to ‘recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])’ 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back C ++错误:没有匹配的函数可以调用&#39;std :: vector <int> ::交换(标准::矢量 <int> )” - C++ Error: no matching function for call to 'std::vector<int>::swap(std::vector<int>)' 错误:没有匹配的 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' 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' C ++线程与以std :: vector为参数的函数调用不匹配 - C++ threads not matching function call with std::vector as argument 指针混淆导致“没有匹配的 function 调用 std::vector”? - Pointer confusion causing "no matching function call for std::vector"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM