我需要將整數從字串保存到向量。
數字的定義:每個完全由數字組成的字串子字串,在該子字串的第一個字符之前有一個空格,在最后一個字符之后有一個空格或標點符號,除非子字串位于字串的開頭或結尾(在在這種情況下,前面不必有空格,也不必在后面有空格或標點符號)。
EXAMPLE 1: "120 brave students, 35 failed, remaining 85..." Numbers: 120, 35, 85
EXAMPLE 2: "2PAC and U2 have class" Numbers: // there aren't any numbers
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
int CharToInt(int c) {
return abs('0' - c);
}
int MakeNumber(std::vector < int > temp) {
int num;
if (temp.size() == 1)
num = temp[0];
if (temp.size() == 2)
num = temp[0] * 10 temp[1];
if (temp.size() == 3)
num = temp[0] * 100 temp[1] * 10 temp[2];
if (temp.size() == 4)
num = temp[0] * 1000 temp[1] * 100 temp[2] * 10 temp[3];
return num;
}
std::vector < int > ExtractNumbers(std::string str) {
std::vector < int > a;
bool found_number;
std::vector < int > temp;
for (int i = 0; i < str.length(); i ) {
// skip spaces
found_number = false;
while (str[i] == ' ' && i < str.length())
i ;
// inside word
while (str[i] != ' ' && i < str.length()) {
while (str[i] >= '0' && str[i] <= '9' && i < str.length()) {
temp.push_back(CharToInt(str[i]));
i ;
found_number = true;
}
i ;
}
if (found_number)
a.push_back(MakeNumber(temp));
temp.clear();
}
return a;
}
int main() {
std::vector < int > a = ExtractNumbers("120 brave students, 35 failed, remaining 85...");
std::vector < int > b = ExtractNumbers("2PAC and U2 have class");
for (int i: a) std::cout << i << " ";
std::cout << std::endl;
for (int i: b) std::cout << i << " ";
return 0;
}
輸出:
120 35 85 // ?
2 2 // ?
您能幫我修改它以使其在示例 2 中正常作業嗎?如何檢查找到數字之前是否有空格和空格/標點符號?
uj5u.com熱心網友回復:
您的要求是,數字后面可能有標點符號,而不是其他字符,因此它們被視為數字,這使問題變得更加困難。
解決這個“決議規則”的一種方法是使用正則運算式。首先,我們將輸入字串拆分為單詞(由空格包圍),然后我們測驗一個單詞是否滿足數量要求。
我們不再使用基于 C 風格字符的決議代碼,而是std::istringstream一次從字串中讀取一個單詞。
最后,無論我們考慮一個數字,我們都用 轉換std::stoi()。
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <cctype>
#include <regex>
using StringVector = std::vector<std::string>;
using I32Vector = std::vector<int32_t>;
static std::regex number_regex(R"(\d [\.\,\:\;\!\?]*)");
inline bool is_number(const std::string& s) {
std::cmatch m;
return std::regex_match(s.c_str(),m,number_regex);
}
I32Vector extract_numbers(const std::string& s) {
I32Vector result;
std::string word;
std::istringstream is{s};
while (!is.eof()) {
is >> word;
std::cout << "[" << word << "]";
if (is_number(word)) {
std::cout << " is a number.";
std::cout.flush();
result.push_back(std::stoi(word));
}
std::cout << std::endl;
}
return result;
}
std::ostream& operator<<(std::ostream& stream,
const I32Vector& v) {
if (v.empty())
stream << "()" << std::endl;
else {
stream << "(" << v[0];
for (size_t i = 1; i < v.size(); i ) {
stream << " " << v[i];
}
stream << ")" << std::endl;
}
return stream;
}
int main (int argc, const char* argv[]) {
StringVector test_strings{
"120 brave students, 35 failed, remaining 85...",
"2PAC and U2 have class",
"120 brave students, 35 failed,2 remaining 85..."
};
for (const auto& s : test_strings) {
I32Vector numbers = extract_numbers(s);
std::cout << s << " => " << numbers << std::endl;
}
return 0;
}
編譯后產生,例如clang -13 -std=c 20 -o numbers numbers.cpp:
120個勇敢的學生,35個失敗,剩下85個... => (120 35 85)
2PAC和U2有課=> ()
uj5u.com熱心網友回復:
那 makeNumber 應該是
int MakeNumber(std::vector <int> temp) {
int num = 0;
for( int digit : temp){
num *= 10;
num = digit;
}
return num;
}
不會改變答案,但可以使用任意數量的數字(假設它適合 int)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465384.html
上一篇:使用cin使我的代碼無法正常作業
下一篇:如何使用POS搜索不同出現的字串
