用find_first_of和find_first_not_of找出string中的数字和字母字符
#include"stdafx.h"
#include<iostream>
#include<string>
//#include "ProgramRunClock.h"
//#include<vector>
//#include<list>
//#include<time.h>
using namespace std;
int main(int argc, char **argv) {
string base = "ab2c3d7R4E6";
string digits, letters;
size_t index = 0;
while (true)
{
index = base.find_first_of("0123456789", index);
if (index == base.npos)break;
digits.push_back(base[index]);
index++;
}
index = 0;
while (true)
{
index = base.find_first_not_of("0123456789", index);
if (index == base.npos)break;
letters.push_back(base[index]);
index++;
}
cout << digits << endl << letters << endl;
return 0;
}
读取字符串(单词)文件筛选出不含特定字母的单词
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
//#include "ProgramRunClock.h"
#include<vector>
//#include<list>
//#include<time.h>
using namespace std;
int main(int argc, char **argv) {
ifstream wordgetin(argv[1]);
string temp;
vector<string> words, eligibleWords;
string comparewith{ "bdfgjpqy" };
while (wordgetin >> temp)words.emplace_back(temp);
for (auto word : words)
{
if (word.find_first_of(comparewith) == word.npos)eligibleWords.emplace_back(word);
}
for (auto word : eligibleWords)
{
cout << word << endl;
}
return 0;
}
在stringvector里计算包含数字的字符串元素转化后的和
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
//#include "ProgramRunClock.h"
#include<vector>
//#include<list>
//#include<time.h>
using namespace std;
float VecTransformSum(vector<string> svec)
{
float sum = 0;
string buffer;
for (auto itr = svec.begin(); itr != svec.end(); itr++)
{
buffer = *itr;
buffer = buffer.substr(buffer.find_first_of("+-.0123456789"));
sum += stof(buffer);
}
return sum;
}
int main(int argc, char **argv) {
vector<string> ivec{"hda1.2","bdax2.4","scaf3.1","lwefeafewagfes4.6","yafafaq5.6"};
cout << VecTransformSum(ivec) << endl;
return 0;
}
一个用string内部函数,将不同类型的日期转化为标准格式的类
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
//#include "ProgramRunClock.h"
#include<vector>
//#include<list>
//#include<time.h>
using namespace std;
class YearMonthDay
{
public:
YearMonthDay() = default;
YearMonthDay(const string &base);
const string getdatestr()
{
stringstream buffer;
string temp, result;
buffer << year; buffer >> temp; result.append(temp + "年"); buffer.clear();
buffer << month; buffer >> temp; result.append(temp + "月"); buffer.clear();
buffer << day; buffer >> temp; result.append(temp+"日");
return result;
}
private:
unsigned year = 1970;
unsigned month = 1;
unsigned day = 1;
};
YearMonthDay::YearMonthDay(const string &base)
{//在这些vector里找月数关键词
string str_year, str_month, str_day;
bool english_month = false;
vector<string> uppercase{ "January","February" ,"March", "April", "May" ,"June" ,"July", "August" ,"September" ,"October" ,"November" ,"December"};
vector<string> lowercase{ "january","february" ,"march", "april", "may" ,"june" ,"july", "august" ,"september" ,"october" ,"november" ,"december" };
vector<string> uppercase_abbreviation{ "Jan","Feb" ,"Mar", "Apr", "May" ,"Jun" ,"Jul", "Aug" ,"Sep" ,"Oct" ,"Nov" ,"Dec" };
vector<string> lowercase_abbreviation{ "jan","feb" ,"mar", "apr", "may" ,"jun" ,"jul", "aug" ,"sep" ,"oct" ,"nov" ,"dec" };
for (size_t index = 0; index != 12; index++)
{//根据下标用find找4个vector中对应的作为base子字符串的元素
size_t search_one = base.find(uppercase[index]),
search_two = base.find(lowercase[index]),
search_three = base.find(uppercase_abbreviation[index]),
search_four = base.find(lowercase_abbreviation[index]);//在四个vector里找到的子串的首字符位置
if (search_one!=base.npos)
{
month = index + 1;
str_month = uppercase[index];
english_month = true;
break;
}
if (search_two != base.npos)
{
month = index + 1;//如果能找到就根据下标给month赋值
str_month = lowercase[index];
english_month = true;
break;
}
if (search_three != base.npos)
{
month = index + 1;
str_month = uppercase_abbreviation[index];
english_month = true;
break;
}
if (search_four != base.npos)
{
month = index + 1;
str_month = lowercase_abbreviation[index];
english_month = true;
break;
}
}
vector<string> years_compare;
stringstream buffer;
string temp;
for (size_t y = 1900; y <= 2100; y++)
{
buffer << y;
buffer >> temp;
years_compare.emplace_back(temp);
buffer.clear();
}//年份vector一共200年 1900-2100
for (size_t index = 0; index <= 200; index++)
{
size_t search = base.find(years_compare[index]);
if (search != base.npos)
{
year = 1900 + index;
str_year = years_compare[index];
break;
}//比较找一下是哪一年,更新year值
}
size_t search_year = base.find(str_year);
if (search_year == base.size() - 4)
{
size_t search_day = base.find_first_of("0123456789");
str_day.push_back(base[search_day]);
if (isdigit(base[search_day + 1]))str_day.push_back(base[search_day + 1]);
stringstream buffer;
buffer << str_day; buffer >> day; buffer.clear();
if (!english_month)
{
size_t month_index = search_day+2;
while (!isdigit(base[month_index]))month_index++;
while (isdigit(base[month_index]))
{
str_month.push_back(base[month_index]);
month_index++;
}
buffer << str_month; buffer >> month;
}
}
else
{
size_t month_index = 5;
while (isdigit(base[month_index]))
{
str_month.push_back(base[month_index]);
month_index++;
}
month_index++;
while (isdigit(base[month_index]))
{
str_day.push_back(base[month_index]);
month_index++;
}
buffer << str_month; buffer >> month; buffer.clear();
buffer << str_day; buffer >> day;
}
}
int main(int argc, char **argv) {
const string testfilelocation = argv[1];
ifstream datetestgetlineinput(testfilelocation);
if (datetestgetlineinput)cout << "创建名为datetestgetlineinput的文件输入流成功,文件位置: " << testfilelocation << endl;
else cout << "创建名为datetestgetlineinput的文件输入流失败" << endl;
string datestr;
while (!datetestgetlineinput.eof())
{
cout << "输入要转化为标准格式的日期字符串: ";
getline(datetestgetlineinput, datestr);
cout << datestr << endl;
YearMonthDay date(datestr);
datestr.clear();
cout << date.getdatestr() << endl;
}
datetestgetlineinput.close();
return 0;
}
不能转化很多类型的日期,只针对这几种
运行结果