删除包含数字的字符串并在上箱中转换其他字符串,然后用逗号分开
我几乎完成了这项任务,但结果有点麻烦。这是问题描述: 给定一个字符串向量,请实现以下2个函数来处理和输出它:
process():删除包含数字的字符串并在上案例中转换其他的字符串
output():打印字符串并用逗号分隔。 提示:
- 如果可能
鼓励利用STL功能/算法。 预期输出: ======================================
之前:ABC,123,456,xyz
之后:abc,xyz
但我的代码不符合预期,
before process: abc, 123, 456, xyz,
after process: ABC, XYZ,
请告诉我如何在两种情况下/之后的情况下删除字符串末尾的逗号,
这是我的代码:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>
void process(std::vector<std::string>& v)
{
// Remove strings that contain digits
for(std::string &s : v)
s.erase(std::remove_if(std::begin(s), std::end(s),
[](unsigned char ch) { return std::isdigit(ch); }),
s.end());
// Convert other characters into upper case
// std::toupper has several overloads
// template <class charT> charT toupper(charT, const locale&)
// int toupper(int ch)
// So taking its address might be complicated.
// We can use lambda to let compiler found the right overload:
// (In addition, as char might be signed or not, and toupper expects unsigned char value (or EOF))
for(std::string &s : v)
{
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::toupper(c); });
}
std::cout << '\n';
}
void output(std::vector<std::string>& v)
{
for(std::string &str : v)
if(str != "\0")
std::cout << str << ", ";
}
int main()
{
std::vector<std::string> v = { "abc", "123", "456", "xyz" };
std::cout << "Before: "; output(v);
process(v);
std::cout << "After: "; output(v);
return 0;
}
I've almost finished this task but have a little trouble in result. Here's the problem description:
Given a vector of strings, please implement the following 2 functions to process and output it:
process(): remove strings that contain digits and convert others in upper case
output(): print the strings and separate them with comma.
Hints:If possible, manipulate the vector elements in place to avoid copying vector.
It is encouraged to leverage STL functions/algorithms.
Expected Output:
==========================================Before: abc, 123, 456, xyz
After: ABC, XYZ
But my code caused the input is not as expected
before process: abc, 123, 456, xyz,
after process: ABC, XYZ,
Please tell me how to remove the comma in the end of string in both case before/after process
Here's my code:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>
void process(std::vector<std::string>& v)
{
// Remove strings that contain digits
for(std::string &s : v)
s.erase(std::remove_if(std::begin(s), std::end(s),
[](unsigned char ch) { return std::isdigit(ch); }),
s.end());
// Convert other characters into upper case
// std::toupper has several overloads
// template <class charT> charT toupper(charT, const locale&)
// int toupper(int ch)
// So taking its address might be complicated.
// We can use lambda to let compiler found the right overload:
// (In addition, as char might be signed or not, and toupper expects unsigned char value (or EOF))
for(std::string &s : v)
{
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::toupper(c); });
}
std::cout << '\n';
}
void output(std::vector<std::string>& v)
{
for(std::string &str : v)
if(str != "\0")
std::cout << str << ", ";
}
int main()
{
std::vector<std::string> v = { "abc", "123", "456", "xyz" };
std::cout << "Before: "; output(v);
process(v);
std::cout << "After: "; output(v);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打印矢量的第一个元素。用下一个元素打印逗号。
Print first element of your vector. Make comma be printed with next elements.
有时,好的旧语法是一件好事。
另一种方法
Sometimes, good old syntax is a good thing.
another method