【问题标题】:concatenate const char * strings连接 const char * 字符串
【发布时间】:2012-07-26 19:57:08
【问题描述】:

我对@9​​87654321@ 和const char * 感到困惑。在我的示例中,我不确定如何将它们组合在一起。我有几个 const char * 字符串我想连接到最终的 const char * 字符串。

struct MyException : public std::exception
{
  const char *source;
  int number;
  const char *cause;

  MyException(const char *s, int n)
      : source(s), number(n) {}
  MyException(const char *s, const char *c)
      : source(s), number(0), cause(c) {}

  const char *what() const throw()
  {
    if (number != 0) {
      char buffer[1024];
      // why does this not work?
      cause = strerror_r(number, buffer, 1024);
    }

    // how to concatenate the strings?
    return source + ": " + cause;
  }
};

【问题讨论】:

  • 为什么要使用 cstrings?你受限于他们吗?我建议你使用 std::string。
  • 这是一个练习吗?如果不是,您可以使用 std::strings 让生活更轻松??
  • 同上,同上。使用标准::字符串。更好地解决 cstrings 的大多数问题。
  • @Zeta:在投掷过程中,它不应该...所有从 std::exception 派生的 类都使用 std::string!

标签: c++ string char constants concatenation


【解决方案1】:

您可以存储 std::string 并仍然从您的 what 函数返回 const char *。

struct MyException : public std::exception
{
private:
  std::string message;  

public:    
  MyException(const char *s, int n) {
    char buffer[1024];
    strerror_r(n, buffer, 1024);
    message.reserve(strlen(s) + 2 + strlen(buffer));
    message = s;
    message += ": ";
    message += buffer;
  }

  MyException(const char *s, const char *c) {
    message.reserve(strlen(s) + 2 + strlen(c));
    message = s;
    message += ": ";
    message += c;
  }

  const char *what() const throw()
  {
    return message.c_str();
  }
};

【讨论】:

    【解决方案2】:

    只需使用 string.h 中的 strcat() 和 strcpy() 函数即可。

    http://www.cplusplus.com/reference/clibrary/cstring/strcat/ http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

    另外,由于您不必修改原始字符串,const char* 和 char* 之间的区别并不重要。

    也不要忘记malloc()(保留空间)目标字符串所需的大小。

    【讨论】:

    • +1 建议使用 strcat 而不是 std:string。我是老派。
    • 你会让捕手负责释放内存吗?
    • 很多时候malloc迫在眉睫。
    • 如果在到达what() 之前丢失了原始字符串怎么办?还是只能与文字和全局变量一起使用?这会创建一个非常糟糕的界面。使用 std::string 可以解决大部分问题。
    • 如果我没记错的话,OP 明确询问了 char* 字符串。
    【解决方案3】:

    这就是我的实现方式:

    struct MyException : public std::exception
    {
    public:
      const char *source;
      int number;
      const char *cause;
    private:
      char buffer[1024]; // #1
      std::string message; // #2
    
      std::string build_message() {
        if (number != 0) {
          cause = strerror_r(number, buffer, 1024); // use the member buffer
        }
        std::string s; // #3
        s.reserve(strlen(source) + 2 + strlen(cause));
        return s + source + ": " + cause;
      }
    
    public:
      MyException(const char *s, int n)
          : source(s), number(n), cause(), message(build_message()) {}
      MyException(const char *s, const char *c)
          : source(s), number(0), cause(c), message(build_message()) {}
    
      const char *what() const throw()
      {
        return message.c_str(); // #4
      }
    };
    

    注意事项:

    1. 原始代码使用 局部变量 作为缓冲区。这是个坏主意,因为存储在cause 中的指针将是invalid the moment the scope ends。

    2. 对于串联的消息,需要动态分配。这也意味着需要清理该存储。我抓住了一个现有的工具来执行此操作并提供类似字符串的操作:std::string。

    3. std::string 的连接可以通过 + 运算符完成。请注意我如何要求它为预期大小保留内存。这是内存优化,不是必需的:无论哪种方式,字符串都会分配足够的内存。

    4. what 不能抛出异常,否则会出现调用 std::unexpected。所以这里不能分配字符串。

    【讨论】:

      【解决方案4】:

      如果您必须使用char* 指针,您将需要使用strcat。 strcat 接受两个参数char* 和const char* 并将const char* 指向的字符串附加到char* 上。这意味着您首先需要复制您的第一个字符串。

      你会想做这样的事情:

      char* Concatenate(const char* first, const char* second)
      {
        char* mixed = new char[strlen(first) + strlen(second) + 2 /* for the ': ' */ + 1 /* for the NULL */];
        strcpy(mixed, first);
        strcat(mixed, ": ");
        strcat(mixed, second);
      
        return mixed;
      }
      

      这不是很丑吗?而且,请记住,因为您已经动态分配了该函数返回的 char*,所以调用者必须记住 delete[] 它。这种丑陋和需要确保调用者以正确的方式清理是为什么你最好使用字符串实现,例如std::string。

      【讨论】:

      • 这不能在what 中调用,因为new 可以抛出。
      【解决方案5】:

      分配大小为strlen(source) + strlen(cause) + 3 的缓冲区并使用sprintf 创建您的消息。其实你可以把这段代码移到构造函数中,这样what就变成了简单的getter。

      【讨论】:

        【解决方案6】:

        如果你真的必须使用 c-strings,你应该查看strcat() 将它们连接在一起。但是,由于您正在创建自定义异常,因此考虑使用 std::string 是合理的,因为它在 C++ 中使用更友好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-28
          • 1970-01-01
          • 2013-10-31
          • 2013-03-25
          • 2010-09-21
          • 2010-12-31
          相关资源
          最近更新 更多