惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

O
OpenAI News
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
罗磊的独立博客
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Attack and Defense Labs
Attack and Defense Labs
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
博客园 - 聂微东
The Last Watchdog
The Last Watchdog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Latest news
Latest news
美团技术团队
Hacker News: Ask HN
Hacker News: Ask HN
J
Java Code Geeks
V
Visual Studio Blog
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security
L
Lohrmann on Cybersecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security Affairs
T
Threatpost
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
博客园 - 三生石上(FineUI控件)
V
V2EX
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Jina AI
Jina AI
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
Scott Helme
Scott Helme
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
H
Hacker News: Front Page
月光博客
月光博客
小众软件
小众软件

Lan小站-嗯,不错! - 学习笔记

滑块验证图片匹配 - Lan小站-嗯,不错! Chrome:此扩展程序不再受支持,因此已停用,Mac解决方案 - Lan小站-嗯,不错! 从 pip 到 uv:一场 Python 包管理的「换引擎」革命 在 Django 中实现基于 JA3 指纹的反爬虫策略 纪念一下,FileCodeBox再次进入Github日榜 - Lan小站-嗯,不错! c++的WS2tcpip和thread的bind冲突怎么解决 - Lan小站-嗯,不错! 根据日期对mysql数据分组之后查询每个日期中的不同状态的数量 - Lan小站-嗯,不错! Chrome 下载 http 进度 卡住 Fast-Crud中rowHandle的remove操作点击无效 - Lan小站-嗯,不错!
C++ string转CString - Lan小站-嗯,不错!
Lan · 2024-06-24 · via Lan小站-嗯,不错! - 学习笔记

在C++ 中,将 std::string 转换为 CString 可以使用以下几种方法。其中最简单和高效的一种是使用 CString 的构造函数进行直接转换。以下是几种常见的方法:

方法一:使用 CString 的构造函数

#include <atlstr.h>  // CString 需要这个头文件
#include <string>

int main() {
    std::string stdStr = "Hello, World!";
    CString cstr(stdStr.c_str());

    // 这里可以对 cstr 进行进一步操作

    return 0;
}

方法二:使用 CString 的赋值运算符

#include <atlstr.h>
#include <string>

int main() {
    std::string stdStr = "Hello, World!";
    CString cstr;
    cstr = stdStr.c_str();

    // 这里可以对 cstr 进行进一步操作

    return 0;
}

方法三:使用 CString 的 Format 方法

#include <atlstr.h>
#include <string>

int main() {
    std::string stdStr = "Hello, World!";
    CString cstr;
    cstr.Format(_T("%s"), stdStr.c_str());

    // 这里可以对 cstr 进行进一步操作

    return 0;
}

总结

最简洁和高效的方式是直接使用 CString 的构造函数,这是因为它能够直接从 std::string 的 C 字符串 (c_str()) 创建 CString 对象。这样避免了额外的步骤和临时对象的创建。

#include <atlstr.h>
#include <string>

int main() {
    std::string stdStr = "Hello, World!";
    CString cstr(stdStr.c_str());

    // 这里可以对 cstr 进行进一步操作

    return 0;
}