[英]How would I get convert this std::string into a std::basic string?
目前我有一個名為cipher_line
的std:::string
,我從以下過程中獲得:
string str_cipher_line;
// Get the Offline Mount Point
ifstream ifs1;
ifs1.open(offlineMountPoint.c_str());
if (ifs1.is_open()) {
getline(ifs1, str_cipher_line);
} else {
cout << "unable to open file" << endl;
return 1;
}
ifs1.close();
現在,我希望能夠得到一個secure_string
從cipher_line
。 secure_string
定義如下:
typedef std::basic_string<char, std::char_traits<char>, zallocator<char> > secure_string;
我不明白如何做到這一點。 我應該使用memcpy
還是strcpy
?
使用std::basic_string
迭代器構造函數(cppreference 上的 6 個)從secure_string
或std::copy
構造。 反過來也一樣。
#include <iostream>
template <typename T>
struct some_other_allocator : std::allocator<T>{};
using other_string = std::basic_string<char, std::char_traits<char>, some_other_allocator<char>>;
int main() {
other_string string1("hello");
//using std::string constructor
std::string string2(string1.begin(),string1.end());
std::string string2_copy;
//using std::copy
std::copy(string1.begin(),string1.end(),std::back_inserter(string2_copy));
std::cout << string1 << std::endl;
std::cout << string2 << std::endl;
std::cout << string2_copy << std::endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.