C++如何将函数参数转换为字符串

标签 c++ string

我在 input 下面有一个函数,它有 4 个参数。在函数内部,我想将函数调用重新创建为字符串并将其写入文件。

例如这个函数可以这样调用:

input = cm.input(mv::Shape(224, 224, 3), mv::DType::Float, mv::Order::Planar);

将参数转换为字符串的最佳方法是什么?之后,我计划将它们连接起来。

我很感激任何关于从哪里开始的建议。

mv::Model::input(const Shape& shape, DType dType, Order order, const string& name)
{
//Create string of function call
}

最佳答案

试试这个:

mv::Model::input(const Shape& shape, DType dType, Order order, const string& name)
{
    std::ostringstream ss;
    ss << "Function [mv::Model::input]- [const Shape&]" << shape.print() << " [DType]" << dType.print()<< " [Order]" << order.print()<< " [const string&]" << name;
    //write to file
    std::ofstream of;
    of.open("file.txt");
    of << ss.rdbuf();
}

为每个对象添加print()成员函数:

    std::string shape::print() const
    { 
       return "round shape";
    }

关于C++如何将函数参数转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51749035/

相关文章:

c - 如何显示字符串表中的元素

Bash 字符串处理(索引和连接处的字符)

linux - Bash - 获取带有颜色代码的子字符串

java - 我应该如何在java中实现子字符串函数?

c++ - GRPC CreateChannel() 错误无法获取默认 pem 根证书

c++ - Google Assistant SDK (C++) - 广播指令

c++ - 在构造函数 C++ 中抛出异常

c++ - boost 正则表达式中的链接器错误

javascript - 如果 url 符合某些条件则重定向用户

c++ - 编译器资源管理器和 GCC 有不同的输出