鱼C论坛

 找回密码
 立即注册
查看: 3503|回复: 11

类型转换问题 求助

[复制链接]
发表于 2022-9-29 18:05:37 | 显示全部楼层 |阅读模式
10鱼币
做了一个转换类, 但是比如说是字符串 中间有空格 就会在空格前截断, 但是如果不用模板类声明 w/stringstream 就是正常的
//根据数据类型 自动选择 stream 的类型是 stringstream 还是 wstringstream
//比如说函数的返回值是wstring, CStringW, wchar_t*, 那么 stream的类型应该是wstringstream
//      那么否则 是string CStringA, char* stream的类型应该是stringstream

#include <string>
#include <iostream>
#include <sstream>

template<typename T>
struct convert_string {
        typedef std::stringstream type;
};

template<>
struct convert_string<wchar_t*> {
        typedef std::wstringstream type;
};

template<>
struct convert_string<const wchar_t*> {
        typedef std::wstringstream type;
};

template<>
struct convert_string<std::wstring > {
        typedef std::wstringstream type;
};

template<>
struct convert_string<const std::wstring > {
        typedef std::wstringstream type;
};

template <typename TRet, typename TParam>
inline TRet Convert(const TParam& t)
{
        typename convert_string<TRet>::type stream;
        TRet tRet;
        stream << t;
        stream >> tRet;
        return tRet;
}

int main()
{
        using namespace std;
        const char* pmsg = "hello world";
        std::stringstream a;
        std::wstringstream b;
        auto ret = Convert<std::string, const char*>(pmsg);
        auto nex = Convert<std::wstring>(L"WH AT");
        std::cout << ret;
        Convert<string>("12 34");
        Convert<string>("hello world");
        Convert<string>(L"1234");
        Convert<int>("1234");
        Convert<int>(L"1234");
        Convert<wstring>("2234");
        Convert<wstring>(L"1234");
        Convert<string>(3.15);
        Convert<wstring>(L"1234");
        return 0;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-29 20:47:24 | 显示全部楼层
没看懂你的问题,把两个代码都贴出来
一个没问题的和一个有问题的,然后说明你的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-30 08:13:00 | 显示全部楼层
人造人 发表于 2022-9-29 20:47
没看懂你的问题,把两个代码都贴出来
一个没问题的和一个有问题的,然后说明你的问题

就是下面 Convert<string>("12 34"); 只会转换12,  34丢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-30 08:40:38 | 显示全部楼层
pediyzhi 发表于 2022-9-30 08:13
就是下面 Convert("12 34"); 只会转换12,  34丢了

这不是很正常么?
你的意思是 你的另外一个代码可以把12,34都转换了?
贴出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-30 08:41:48 | 显示全部楼层
pediyzhi 发表于 2022-9-30 08:13
就是下面 Convert("12 34"); 只会转换12,  34丢了

但是如果不用模板类声明 w/stringstream 就是正常的
你把 不用模板类声明 w/stringstream 的版本发出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-2 07:29:03 | 显示全部楼层
人造人 发表于 2022-9-30 08:41
但是如果不用模板类声明 w/stringstream 就是正常的
你把 不用模板类声明 w/stringstream 的版本发出来 ...

不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 10:34:45 | 显示全部楼层
.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 11:18:36 | 显示全部楼层
pediyzhi 发表于 2022-10-2 07:29
不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello

看不懂你的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 11:20:04 | 显示全部楼层
pediyzhi 发表于 2022-10-2 07:29
不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello

你不是说 不用模板类声明 w/stringstream 就是正常的 么?
你把这个正常的代码发出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-2 17:44:09 | 显示全部楼层
人造人 发表于 2022-10-2 11:20
你不是说 不用模板类声明 w/stringstream 就是正常的 么?
你把这个正常的代码发出来看看

好奇怪. 我现在正常的也不正常了.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-6 21:51:47 | 显示全部楼层
题在第41行
stream流在传递给tRet的时候,默认只传递第一个空格前的内容,可以考虑使用循环
借用一个临时变量tf 或许可以这么写:
TRet tf;
while(stream >> tf) {
    tRet += tf;
    tRet += ' '; //保留空格
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-7 20:10:43 | 显示全部楼层
这段代码有一个问题,它在处理带空格的字符串时会截断空格之前的部分。这是因为默认情况下`operator>>`会停止在空格处。要解决这个问题,可以使用`std::getline()`函数而不是`operator>>`来读取输入,因为它可以读取整行文本,包括空格。

以下是修改后的示例代码:
#include <string>
#include <iostream>
#include <sstream>

template<typename T>
struct convert_string {
        typedef std::stringstream type;
};

template<>
struct convert_string<wchar_t*> {
        typedef std::wstringstream type;
};

template<>
struct convert_string<const wchar_t*> {
        typedef std::wstringstream type;
};

template<>
struct convert_string<std::wstring > {
        typedef std::wstringstream type;
};

template<>
struct convert_string<const std::wstring > {
        typedef std::wstringstream type;
};

template <typename TRet, typename TParam>
inline TRet Convert(const TParam& t)
{
        typename convert_string<TRet>::type stream;
        TRet tRet;
        // 读取整行文本
        std::getline(stream, tRet);
        return tRet;
}

int main()
{
        using namespace std;
        const char* pmsg = "hello world";
        std::stringstream a;
        std::wstringstream b;
        auto ret = Convert<std::string, const char*>(pmsg);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-20 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表