【发布时间】:2022-01-24 03:00:26
【问题描述】:
#include <iostream>
#include <string.h>
#include <strings.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
#include <algorithm>
#include <iterator>
#include <list>
int main(){
char buffer[32] = {0};
std::string temp;
std::string apend;
//memset(buffer, '\0', sizeof(buffer));
std::cout << "Text in: \n";
fgets(buffer, 32, stdin);
for(int i = 0; i < strlen(buffer); ++i){
for(int j = 0; j < strlen(buffer); ++j){
if(buffer[i] == ' '){
buffer[i] = buffer[i+1];
}
}
}
for(int i=0; i < strlen(buffer); ++i){
std::cout << buffer[i];
temp += buffer[i];
}
reverse(temp.begin(), temp.end());
std::cout << temp << std::endl;
std::cout << "Enter a new string: \n" << std::endl;
std::cin >> apend;
temp.append(apend);
for(auto i = temp.begin(); i != temp.end(); ++i){
std::cout << *i <<" ";
}
std::vector<std::string>::iterator ptr1 = temp.begin();
在'ptr1 = temp.begin() 处有一条错误消息说没有合适的用户定义转换...,我就是无法处理这个问题。有人可以帮忙看看我的做法吗?谢谢!
return -1;
}
【问题讨论】:
-
您可以删除几乎所有的代码,但仍然会显示问题。不要用这些噪音浪费人们的时间。
-
您需要使用正确的类型。
decltype(temp)::iterator ptr1 = temp.begin();- 也就是说,ptr1是std::string::iterator,而不是std::vector<std::string>::iterator。 -
#include <bits/stdc++.h>-- 摆脱这个。<strings.h>是什么?此外,不需要重复调用strlen以从字符串中删除空格的低效双嵌套for循环。 -
@Embeddeder 我的意思是正是,正如我写的那样。
-
我上面写的没有
auto。我在下面的回答中使用了auto。两者都导致ptr1成为std::string::iterator。
标签: c++