class Solution { public: string dayOfTheWeek(int day, int month, int year) { string names[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int total = 0; for (int i...
bool型函数指的是返回值为bool类型的函数,其调用方式和int 型函数没有太大的区别。bool型变量的值只有 真 (true) 和假 (false)。bool可用于定义函数类型为布尔型,函数里可以有 return true; return false 之类的语句。示例:#include<iostream> using namespace std; bool cmp(int a,int b){ if(a > b){ return true; }else{ return false; } } int main() ...
#include <iostream> using namespace std; #include <regex> regex r(R"(^^\s*[-+]?((\d+(\.\d+)?)|(\d+\.)|(\.\d+))(e[-+]?\d+)?\s*$)"); class Solution { public: bool isNumber(const string& s) { return regex_match(s, r); } }; int main() { system("ch...
#include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<string> data; data.insert("13900000000"); data.insert("13900000000"); data.insert("13988888888"); for (const auto &n: data) { ...
#include <iostream> #include <vector> #include <string> #include<sstream> using namespace std; class Solution { public: int daysBetweenDates(string date1, string date2) { return abs(getDays(date1) - getDays(date2)); } private: int getDays(string dat...
#include <iostream> #include <vector> #include <string> #include<sstream> using namespace std; vector<string> split(const string& text, char delimiter) { string tmp; vector<string> stk; stringstream ss(text); while(getline(ss,tmp, delimi...
vector就是类似于一个数组的容器,内容比数组更加全面。很多操作都有自己的函数可以直接拿过来进行使用。主要函数就是:v.push_back(k); 尾插元素; v.insert(it,k); 在任意位置插入元素; v.eraser(it,it+k); 删除任意元素; v.size(); 容量; v.clear(); 清空容器; v.empty(); 判断容器是否为空; reverse(v.begin(),v.end());反转任意段元素 sort(v.begin(),v.end(),cmp);sort排序默认由小到大,cmp可以自由规定排序规则。 迭代器声明:vector::itera...
在C ++中,我们使用set,multiset,unordered_multiset,unordered_set存储哈希集。C ++ set / multiset实现了一个Red-Black树,该树维护元素的顺序。另一方面,unordered_set和unordered_multiset基于Hashmap / Hashtable,因此不对元素进行排序。multiset和unordered_multiset允许将重复项存储在集中。要遍历集合,我们可以使用简单的for循环:#include <iostream> #include <unordered_set> ...
先介绍一下auto、const: 在块作用域、命名作用域、循环初始化语句等等 中声明变量时,关键词auto用作类型指定符。 const:修饰符 接下来我们细细分析一下:(1)auto auto即 for(auto x:range) 这样会拷贝一份range元素,而不会改变range中元素; 但是!(重点) 使用for(auto x:vector<bool>)时得到一个proxy class,操作时会改变vector<bool>本身元素。应用:for(bool x:vector<bool>) (2)auto& 当需要修改...
/** * @param {string} s * @return {string} */ var reverseWords = function(s) { return s.trim().split(' ').reverse().filter(x => x.length > 0).join(' '); }; 例如:I LOVE YOU结果:YOU LOVE I