R
Ryan
I have a template method that extracts the file name from a string. The string can either be a char or wchar_t type. I would like to define the value "/\\" as the constructor value of my basic_string<T>. I'm currently having to define the value "/\\" as a basic_string<char> and using its iterators as an input for the basic_string<T>. Is it possible to use "/\\" value directly in the basic_string<T>?
Ryan
#include <string>
#include <iostream>
template<typename T>
std::basic_string<T> getFileName(
std::basic_string<T> const& pathAndFileName) {
std::basic_string<char> pattern("/\\");
return pathAndFileName.substr(
pathAndFileName.find_last_of(
std::basic_string<T>(pattern.begin(), pattern.end())) + 1);
}
int main(int, char const *[]) {
std::basic_string<char> string1("sub\\directory\\file.txt");
std::string result1 = getFileName(string1);
std::cout << result1 << std::endl;
std::basic_string<wchar_t> string2(L"sub\\directory\\file.txt");
std::basic_string<wchar_t> result2 = getFileName(string2);
std::wcout << result2 << std::endl;
return 0;
}
Ryan
#include <string>
#include <iostream>
template<typename T>
std::basic_string<T> getFileName(
std::basic_string<T> const& pathAndFileName) {
std::basic_string<char> pattern("/\\");
return pathAndFileName.substr(
pathAndFileName.find_last_of(
std::basic_string<T>(pattern.begin(), pattern.end())) + 1);
}
int main(int, char const *[]) {
std::basic_string<char> string1("sub\\directory\\file.txt");
std::string result1 = getFileName(string1);
std::cout << result1 << std::endl;
std::basic_string<wchar_t> string2(L"sub\\directory\\file.txt");
std::basic_string<wchar_t> result2 = getFileName(string2);
std::wcout << result2 << std::endl;
return 0;
}