【发布时间】:2020-05-29 14:44:14
【问题描述】:
我要解决的问题是:我必须采取 T 测试用例。对于每个测试用例,我必须将字符串作为输入,然后我需要将输入字符串排列为:偶数位置的字符串 {double space} 奇数位置的字符串(例如:input - StackOverflow,输出 - Sakvrlw tcOefo)。我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。然后我将 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;
}
在编译上述代码时,我得到了"no matching error for call std::vector..."。
我到底做错了什么?
【问题讨论】:
-
even是字符串的字符,s[i]不是字符串。请在问题中包含完整的错误消息,它应该告诉您代码中有什么问题,如果您不理解它可以询问它的含义,但无论如何您应该将其包含在问题中 -
您为什么不显示完整的错误消息 - 可以帮助我们帮助您的东西?
-
如果这是 Visual Studio,请从“输出”选项卡中复制错误消息的文本。错误列表中的错误消息通常是缩写的,并且格式不适合复制/粘贴文本。
标签: c++ string loops vector c++14