[英]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
.发生这种情况是因为
even
是vector<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>
.如果我正确理解了您的问题,则
even
和odd
都必须是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 astd::string
or an array ofchar
ss
是std::string
或char
数组
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>
或char
到std::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.