86 lines
2.0 KiB
C++

#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> words;
std::string word;
while (iss >> word) {
words.push_back(word);
}
return words;
}
void ConvertStringVectorToIntVector(const std::vector<std::string> &strVec,
std::vector<int> &intVec) {
std::transform(strVec.begin(), strVec.end(), std::back_inserter(intVec),
[](const std::string &s) { return std::stoi(s); });
}
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &TwoDCharVec) {
if (x < 0 || y < 0) {
return false;
}
if (x >= TwoDCharVec.size() || y >= TwoDCharVec[x].size()) {
return false;
}
return true;
}
int condop(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
case 'c':
return std::stoi(std::to_string(a) + std::to_string(b));
default:
return a;
}
}
unsigned long long condop_ull(unsigned long long a, unsigned long long b,
char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
case 'c':
return std::stoull(std::to_string(a) + std::to_string(b));
default:
return a;
}
}
std::vector<std::string> splitStringByChar(const std::string &str, char c) {
std::vector<std::string> words;
std::string word;
std::istringstream iss(str);
// Use std::getline with ',' as the delimiter
while (std::getline(iss, word, c)) {
// Trim whitespace from the beginning and end of the word
size_t start = word.find_first_not_of(" \t");
size_t end = word.find_last_not_of(" \t");
if (start != std::string::npos && end != std::string::npos) {
words.push_back(word.substr(start, end - start + 1));
}
}
return words;
}