使用 for_each ,使用字符串复制
Using for_each , copy with string
我对程序有一些问题,也许有人可以帮助我。所以:
int main() {
std::string col = "maly tekst"
for_each(/* FILL IN #2*/ f());
copy(/*FILL IN #3*/);
std::cout << col; }
输出应为:TSKET YLAM
我知道我需要使用函子,所以我做了这样的东西:
#include <iostream>
#include <string>
#include <algorithm>
class f{
public:
void operator() (char &k)const
{
k = toupper(k);
}
};
int main(){
std::string col = "maly tekst";
for_each(col.begin(),col.end(),f());
copy(col.rbegin(),col.rend(),back_inserter(col));
std::cout << col << std::endl;
}
但是现在当我运行它时,它返回:
MALY TEKSTTSKET YLAM
有人可以指出我正确的方法,或者帮助我使用此示例程序吗?
谢谢
E:忘了补充一下我只能在main中使用这个功能,我不能添加任何新内容
std::for_each(col.begin(),col.end(),f()); // as before
std::reverse(col.begin(), col.end());
如果您不能使用 std::copy 来替换原始容器,则不能使用 std::reverse
。要打印带有reverse order
的 col,另一种解决方法是直接将col
复制到stream iterator
。
for_each(col.begin(),col.end(),f());
std::copy(col.rbegin(), col.rend(), std::ostream_iterator<char>(std::cout, ""));
相关文章:
- 使用 strcpy_s 将字符串复制到字符*
- 在这种情况下,使用 string_view 是否会导致不必要的字符串复制?
- 无法使用 strcpy 将字符串复制到静态结构成员
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 将 C 样式字符串复制到免费存储上分配的内存中
- strcpy_s将超过字符串长度的字符串复制到目标缓冲区(用 0xFE 填充)
- 将字符串复制到类中的常量字符
- 使用 copy_if 复制 std::字符串(复制到另一个字符串中)
- 将字符串复制到C 中的另一个字符串并打印其字符
- Linux/C++ 将字符串复制到剪贴板
- 将字符串复制到 char* 的现代C++方法
- 从一个字符串复制到另一个字符串并粘贴到新文件中
- 如何才能将字符串复制到带有 std::string 字段的结构中
- 如何将数字列表从字符串复制到C++向量
- 通过指针将字符串复制到结构
- 如何将包含多个空字符的字符串复制到字符串流
- 为什么我不能从字符串复制初始化字符串流?
- 如何做字符串::复制C++的反义词
- 即使在将新字符串复制到现有字符串之后,旧包含也可用(在某些索引中)
- c无法将字符串复制到文件中