删除包含数字的字符串并在上箱中转换其他字符串,然后用逗号分开

发布于 2025-02-03 05:58:47 字数 1862 浏览 2 评论 0原文

我几乎完成了这项任务,但结果有点麻烦。这是问题描述: 给定一个字符串向量,请实现以下2个函数来处理和输出它:

  1. process():删除包含数字的字符串并在上案例中转换其他的字符串

  2. output():打印字符串并用逗号分隔。 提示:

  3. 如果可能
  4. 鼓励利用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:

  1. process(): remove strings that contain digits and convert others in upper case

  2. output(): print the strings and separate them with comma.
    Hints:

  3. If possible, manipulate the vector elements in place to avoid copying vector.

  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2025-02-10 05:58:47

打印矢量的第一个元素。用下一个元素打印逗号。

void output(const std::vector<std::string> &v) {
    // find 1st not-empty string
  auto iter = std::find_if(v.cbegin(), v.cend(),
                           [](const auto &s) { return !s.empty(); });

  if (iter == v.cend()) {
    return;  // return if all of v are empty strings
  }

  std::cout << *iter;  // print 1st one
  ++iter;

  // print comma and next string if not empty
  for (; iter != v.cend(); ++iter) {
    if (!iter->empty()) {
      std::cout << ", " << *iter;
    }
  }

  std::cout << '\n';
}

Print first element of your vector. Make comma be printed with next elements.

void output(const std::vector<std::string> &v) {
    // find 1st not-empty string
  auto iter = std::find_if(v.cbegin(), v.cend(),
                           [](const auto &s) { return !s.empty(); });

  if (iter == v.cend()) {
    return;  // return if all of v are empty strings
  }

  std::cout << *iter;  // print 1st one
  ++iter;

  // print comma and next string if not empty
  for (; iter != v.cend(); ++iter) {
    if (!iter->empty()) {
      std::cout << ", " << *iter;
    }
  }

  std::cout << '\n';
}
写给空气的情书 2025-02-10 05:58:47

有时,好的旧语法是一件好事。

void output(std::vector<std::string>& v)  {
    
    // clean array
    size_t n = v.size();
    while(n--) if(v[n] == '\0') v.erase(v.begin() + n);
    
    // print words
    for(size_t i = 0; i < v.size(); ++i) {
        std::cout << v[i];
        if(i < (v.size() - 1) std::cout << ", "; 
    }
}

另一种方法

void output(std::vector<std::string>& v)  {

    // clean array
    size_t n = v.size();
    while(n--) if(v[n] == '\0') v.erase(v.begin() + n);

    // print words
    size_t max = v.size() - 1;

    for(size_t i = 0; i < max; ++i) 
        std::cout << v[i] << ", "; 

    std::cout << v[max];
}

Sometimes, good old syntax is a good thing.

void output(std::vector<std::string>& v)  {
    
    // clean array
    size_t n = v.size();
    while(n--) if(v[n] == '\0') v.erase(v.begin() + n);
    
    // print words
    for(size_t i = 0; i < v.size(); ++i) {
        std::cout << v[i];
        if(i < (v.size() - 1) std::cout << ", "; 
    }
}

another method

void output(std::vector<std::string>& v)  {

    // clean array
    size_t n = v.size();
    while(n--) if(v[n] == '\0') v.erase(v.begin() + n);

    // print words
    size_t max = v.size() - 1;

    for(size_t i = 0; i < max; ++i) 
        std::cout << v[i] << ", "; 

    std::cout << v[max];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文