出自美团2020年春季真题,第7题
前一阵子的家庭时光转眼已经逝去3天了。😠 眼看着日子一天天逝去,感觉自己所获不多… 🈂️ 离秋招越来越近了,内心越来越焦虑。学一点是一点吧,不管了~
美团秋招真题
给字符串逆序排序。
思路:重载string的比较操作符,再采用快速排序结构即可。
- 重载操作符如下:
operator>=
- 与字符串排序相比,仅需要把快排中的比较函数替换成重载后的操作符即可
c++ 重载运算符
c++的运算符重载
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
以+号运算符为例,通常声明如下:
template <typename T>
T operator+(const T &a,const T &b)
{
}
例子如下:
#include<iostream>
#include<string>
using namespace std;
struct Point2d{
double x,y;
// 默认构造
Point2d (){this->x = 0;this->y =0;};
// 构造函数
Point2d (int _x,int _y):x(_x),y(_y){ };
// 加法重载
Point2d operator+(const Point2d& pt)
{
Point2d res;
res.x = this->x +pt.x;
res.y = this->y +pt.y;
return res;
}
// 大于号载入,先比较x坐标
bool operator>(const Point2d& pt)
{
return (this->x > pt.x) ? true : (this->x == pt.x && this->y > pt.y );
}
};
//// 外部定义
bool operator<(const Point2d& pt1, const Point2d& pt2)
{
return (pt1.x < pt2.x) ? true : (pt1.x == pt2.x && pt1.y < pt2.y ); //x1<x2或者,x1=x2并且,y1<y2时,认为pt1更小
}
int main()
{
Point2d pt1(10,5);
Point2d pt2(5,9);
// 加法
auto sum = pt1+pt2;
cout<<sum.x<<" "<<sum.y<<endl;
// 内部比较大小
bool cmp1 = pt1>pt2;
cout<< "inner:"<<cmp1<<endl;
// 外部比较大小
bool cmp2 = pt1<pt2;
cout<<"outside:"<< cmp2 <<endl;
}
输出如下:
可重载的运算符列表
附:c++输入流小知识
istringstream
可以将某段字符串自动按照空格分段,如下代码测试结果:
//
// Created by wbzhang on 2020/8/10.
//
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main()
{
// string inStr;
// getline(cin,inStr);
string str2= "hello world ni hao ya";
istringstream is(str2);
string s;
while(is>>s)
{
cout<<s<<endl;
}
}
// string str= "hello,world,ni,hao,ya";
某位大神的代码如下:
// https://www.nowcoder.com/question/next?pid=21910835&qid=894528&tid=35770036
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
//void swap(string& a, string& b) {
// string tmp;
// tmp = a;
// a = b;
// b = tmp;
//}
bool operator>=(const string &left, const string &right)
{
if (left == "" && right != "")
{
return true;
}
else if (left != "" && right == "")
{
return false;
}
else if (left.size() <= right.size() && left == right.substr(0, left.size()))
{
return true; //left為right的前綴
}
else if (left.size() > right.size() && right == left.substr(0, right.size()))
{
return false; //right为left的前缀
}
else
{
return left > right;
}
}
// 快排
void sortstr(vector<string> &strs, int l, int r)
{
int left = l, right = r;
if (l > r)
{
return;
}
string sign = strs[left];//pivot
while (left < right)
{
/*while (strs[right].compare(sign) && left<right) {
right--;
}
while (sign.compare(strs[left]) && left < right) {
left++;
}*/
while (strs[right] >= sign && left < right)
{
right--;
}
while (sign >= strs[left] && left < right)
{
left++;
}
if (left < right)
{
swap(strs[left], strs[right]);
}
}
swap(strs[l], strs[left]);
sortstr(strs, l, left - 1);
sortstr(strs, left + 1, r);
}
int main()
{
vector<string> strs;
string word, line;
while (getline(cin, line))
{
if (line.size() == 0)
break;
istringstream record(line);
for (int i = 0; i < line.size(); i++)
{
if (line[i] == ',')
{
strs.push_back(word);
word = "";
}
else if (i == line.size() - 1)
{
word += line[i];
strs.push_back(word);
word = "";
break;
}
else
{
word += line[i];
}
}
sortstr(strs, 0, strs.size() - 1);
for (int j = strs.size() - 1; j >= 0; j--)
{
if (j > 0)
cout << strs[j] << ',';
else
cout << strs[j] << endl;
}
}
}
// abcde,waimai,dache,lvyou,liren,meishi,jiehun,lvyoujingdian,jiaopei,menpiao,jiudian