string_view
原型:
template<class CharT, class Traits = std::char_traits<CharT>> class basic_string_view;
1. 示例
1 #include <iostream> 2 #include <string_view> 3 4 int main() { 5 std::string_view sv("123456789", 5); 6 7 for (auto it = sv.cbegin(); it != sv.cend(); ++it) { 8 std::cout << *it << " "; 9 } 10 std::cout << std::endl; 11 12 std::cout << "size() = " << sv.size() << std::endl; 13 std::cout << "data() = " << sv.data() << std::endl; 14 std::cout << "sv.front() = " << sv.front() << std::endl; 15 std::cout << "sv.back() = " << sv.back() << std::endl; 16 return 0; 17 }
输出:
1 2 3 4 5 size() = 5 data() = 123456789 sv.front() = 1 sv.back() = 5
#include <iostream> #include <algorithm> #include <string_view> int main() { std::string str = " trim me"; std::string_view v = str; v.remove_prefix(std::min(v.find_first_not_of(" "), v.size())); std::cout << "String: '" << str << "' " << "View : '" << v << "' "; }