Last active
November 10, 2022 18:31
-
-
Save kLiHz/339dd849650f444c8978520c7d2e8159 to your computer and use it in GitHub Desktop.
C++: `std::string` convenient functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <string> | |
#include <numeric> | |
auto prettify(std::vector<std::string> const& v) { | |
return "[" | |
+ (v.empty() ? "" : ("'" + v.front() + "'" | |
+ std::accumulate( v.begin() + 1, v.end(), std::string(), | |
[](std::string& s, auto const & t){ return std::move(s) + ", '" + t + "'";} ))) | |
+ "]"; | |
} | |
#include <iostream> | |
#include "utils.hpp" | |
int main() { | |
auto v = split(" "); | |
std::cout << prettify(split(" ")) << "\n"; | |
std::cout << prettify(split(" 123 456 789 ")) << "\n"; | |
std::cout << prettify(split("123 456 789 ")) << "\n"; | |
std::cout << prettify(split(" 123 456 789")) << "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <iomanip> | |
#include "utils.hpp" | |
int main() { | |
std::vector<std::string> vec = { | |
"monkey on the tree", | |
" castle on the cloud", | |
"pig fly in the sky ", | |
" the people are singing ", | |
}; | |
for (auto const& item : vec) { | |
std::cout << std::quoted(trimmed(item)) << "\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MY_CPP_STRING_UTILS_H | |
#define MY_CPP_STRING_UTILS_H | |
#include <string> | |
inline std::string trimmed(std::string const& str) { | |
// _ a b c d e f _ _ ^ | |
// 0 1 2 3 4 5 6 7 8 9 | |
auto beginning = str.find_first_not_of(" \t"); | |
auto ending = str.find_last_not_of(" \t"); | |
if (beginning == std::string::npos) { beginning = 0; } | |
if (ending == std::string::npos) { | |
return str.substr(beginning); | |
} else { | |
return str.substr(beginning, ending - beginning + 1); | |
} | |
} | |
#include <string> | |
#include <vector> | |
inline auto split(std::string const & s) { | |
std::vector<std::string> result(1); | |
for (auto const& t : s) { | |
if (t != ' ') { | |
result.back().push_back(t); | |
} else if (!result.back().empty()) { | |
result.emplace_back(); | |
} | |
} | |
if (result.back().empty()) result.pop_back(); | |
return result; | |
} | |
inline auto split(std::string const & s, char c) { | |
std::vector<std::string> result(1); | |
for (auto const& t : s) { | |
if (t == c) { | |
result.emplace_back(); | |
} else { | |
result.back().push_back(t); | |
} | |
} | |
return result; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment