如何从字符串位置构造CSTRING/STD :: String
How to construct a CString/std::string from a string position
给定以下情况:
for( std::string line; getline( input, line ); )
{
CString strFind = line.c_str();
int n = strFind.ReverseFind( '' );
CString s = CString( strFind,n );
cout << s << endl;
// m_Path.push_back( line.c_str() );
}
它正在读取.ini配置,因此,我有一条线:
c: downloads insanity program 7。world.exe
此行添加到vector<CString>
中。
我的问题是 int n = strFind.ReverseFind( '' );
找到了第一个从字符串末端到开始的第一个 search的字符串pos,在构造像这样的 CString s = CString( strFind,n );
的cstring之后,我在字符串上构造了第一个n个字符,因此 s
是相等的 C:DownloadsInsanityProgram
但是我想要的是将7 .World.exe
复制到CCSTRING s
,而不是相反,我该如何使用CString
或std::string
?
您是否仅针对ReverseFind
功能将std::string
转换为CString
?如果是这样,您可以改用std::basic_string::find_last_of
。
#include <iostream>
#include <string>
int main()
{
std::string s(R"(C:DownloadsInsanityProgram7. World.exe)");
auto pos = s.find_last_of( '' ) + 1; //advance to one beyond the backslash
std::string filename( s, pos );
std::cout << filename << std::endl;
}
怎么样:
CString s = strFind.Mid(n+1);
或:
std::string s = line.substr(n+1);
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- C++从 std::string 创建 CString 失败
- CString to std::string or sql::SQLString conversion - C++
- sprintf如何使用CString和std::string
- 如何从字符串位置构造CSTRING/STD :: String
- 快速将 CString 移动到 std::string
- 如何在 unicode 项目中将 std:string 转换为 CString
- 将 ATL::CString 转换为 std::vector<unsigned char>
- CString to std::cout
- 将std::wstring转换为cstring安全吗?
- <CString> 使用 C++ / STL 将 std::vector 展平为multi_sz的推荐方法是什么
- 如何知道std::string/Cstring中的值是否为数字