我如何将此 std::string 转换为 std::basic 字符串?
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();
现在我希望能够从 cipher_line
得到一个 secure_string
。 secure_string
定义如下:
typedef std::basic_string<char, std::char_traits<char>, zallocator<char> > secure_string;
我不明白该怎么做。我应该雇用 memcpy
还是 strcpy
?
使用 std::basic_string
iterator constructor(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;
}
目前我有一个名为 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();
现在我希望能够从 cipher_line
得到一个 secure_string
。 secure_string
定义如下:
typedef std::basic_string<char, std::char_traits<char>, zallocator<char> > secure_string;
我不明白该怎么做。我应该雇用 memcpy
还是 strcpy
?
使用 std::basic_string
iterator constructor(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;
}