[C++] 自定义type_name() 判断数据类型

文章提供了一个名为lib_string的C++库,包含一个静态成员函数replace_all用于字符串替换,以及一个type_name模板函数,用于获取变量或对象的类型名称。示例代码展示了如何使用这些功能,包括对字符串、容器(如map和set)及栈的类型名提取。
摘要由CSDN通过智能技术生成

1. lib_string.h

#ifndef CPP_STRING_H
#define CPP_STRING_H

#include <iostream>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif




using std::string;
using std::remove_reference, std::unique_ptr;
using std::is_const, std::is_volatile, std::is_lvalue_reference, std::is_rvalue_reference;


#define Tmp_ template <class T>
#define _cs co::str




namespace co {
    // using other name!!!
    #define type_name(T) str::_type_name(T)
    class str {
    public:
        static string &replace_all(string &src, const string &_old, const string &_new);
        Tmp_
        static string _type_name(T obj) {
            return _cs::__type_name<decltype(obj)>();
        };
    private:
        Tmp_
        static string __type_name();
    };
}



string& _cs::replace_all(string& src, const string& _old, const string& _new) {
    // 每次重新定位起始位置,防止上轮替换后的字符串形成新的_old
    for (string::size_type pos(0); pos != string::npos; pos += _new.length()) {
        if ((pos = src.find(_old, pos)) != string::npos) {
            src.replace(pos, _old.length(), _new);
        } else break;
    }
    return src;
}



Tmp_
string _cs::__type_name() {
    typedef typename remove_reference<T>::type TR;
    unique_ptr<char, void (*)(void *)> own(
    #ifndef _MSC_VER
            abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
    #else
            nullptr,
    #endif
            free);
    string r = own != nullptr ? own.get() : typeid(TR).name();
    if (is_const<TR>::value)
        r += " const";
    if (is_volatile<TR>::value)
        r += " volatile";
    if (is_lvalue_reference<T>::value)
        r += "&";
    else if (is_rvalue_reference<T>::value)
        r += "&&";
    // string vector map set stack
    if (r.starts_with("std::__1::")) {
        int start = r.find("std::__1::");
        int end = r.find(", std::__1::");
        r = r.substr(start+10, end-start-10) + ">";
    }

    return r;
}


#endif //CPP_STRING_H

2.lib_string-example.cpp

#include "lib_string.h"
//#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <stack>


int main() {


    // replace_all(s, old, new)
    string s = "hello world";
    co::str::replace_all(s, "wo", "212w");  // hello 212wrld
    std::cout << s << std::endl;

    // type_name()

    // string
    std::cout << co::type_name(s) << std::endl;       // basic_string<char>
    std::cout << co::type_name("hello") << std::endl;      // char const*

    // map
    std::unordered_map<char, int> map = {{'a', 3}, {'b', 2}};
    std::cout << co::type_name(map) << std::endl;     // unordered_map<char, int>

    // set
    std::unordered_set<int> set = { 1, 2 };                     // unordered_set<int>
    std::cout << co::type_name(set) << std::endl;

    // stack
    std::stack <int> stack1;
    std::cout << co::type_name(stack1) << std::endl;  // stack<int>


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值