将小写字符变为大写字符
将大写字符变为小写字符
#include < algorithm>
string input = "feng,hao,jun";
stringstream ss(input);
string str;
vector<string> strs;
#getline,将数据流中的字符以","为间隔传入str
while (getline(ss, str, ',')) {
strs.push_back(str);
}
string s = "a,b,c";
replace(s.begin(), s.end(), ',', ' ');
#include
#include
#include
using namespace std;
int main()
{
string str = "world";
const char* p = str.c_str();//同上,要加const或者等号右边用char*
cout << *p << endl;
return 0;
}
将数字变为string类型
将字符串转换为数字
#include
#include
int main()
{
std::map<int, std::string> c = {
{1, "one" }, {2, "two" }, {3, "three"},
{4, "four"}, {5, "five"}, {6, "six" }
};
// erase all odd numbers from c
for(auto it = c.begin(); it != c.end(); ) {
if(it->first % 2 != 0)
it = c.erase(it);
else
++it;
}
for(auto& p : c) {
std::cout << p.second << ' ';
}
}
two four six
求数组中最大元素
vector<int> Arr{1,2,3,4,5,3,2,1};
auto temp = std::max_element(Arr.begin(),Arr.end());
int res = std::distance(Arr.begin(),temp);//最大值在数组中的索引
cout<<Arr[res]<<endl;
与max_element类似
#include
vector<int> A{3,1,2};
//默认升序
sort(A.begin(),A.end());
//自定义降序
sort(A.begin(),A.end(),[](int a,int b){return a > b;});
//非基础变量的排序
vector<pair<int, int>> vec;
vec.push_back(pair<int, int>(1, 3));
vec.push_back(pair<int, int>(2, 1));
vec.push_back(pair<int, int>(3, 2));
//按pair对的第二个元素降序排列
sort(vec.begin(), vec.end(), [](pair<int, int> P1, pair<int, int> P2){return P1.second > P2.second; });
for (auto aa : vec)
cout << aa.first << " ";
return 0;
找到大于某值得第一个值,返回地址
upper_bound(ptr1,ptr2,k);//ptr1指针1,ptr2指针2;k待查值
找到大于等于某值得一个值得指针
lower_bound(ptr1,ptr2,k);//ptr1指针1,ptr2指针2;k待查值
#include