跳到主要内容

库洛

1

// 材质管理器
class TextureManager {
std::unordered_map<std::string, std::shared_ptr<Texture>> _cache;
std::mutex _mutex;
public:
std::shared_ptr<Texture> get(const std::string& name) {
std::lock_guard<std::mutex> lock(_mutex);
auto it = _cache.find(name);
if (it != _cache.end()) {
return it->second;
}
auto texture = std::make_shared<Texture>(name);
_cache[name] = texture;
return texture;
}
void UnloadUnused() {
std::lock_guard<std::mutex> lock(_mutex);
for (auto it = _cache.begin(); it != _cache.end();) {
if (it->second.use_count() == 1) {
it = _cache.erase(it);
} else {
++it;
}
}
}
};
  1. 为什么用了 std::shared_ptr 而不是 std::unique_ptr
  2. UnloadUnused 函数的作用是什么?

2

2.1

64 位机器,默认内存对齐。

struct P {
bool isActive;
float x[3];
char name[4];
bool isVisible;
float y[3];
P * xxx;
};
  1. 请你优化这个结构体的内存布局,减少内存占用。并给出优化前后的内存占用。

2.2

class Point2D {
public:
float x;
float y;
virtual ~Point2D() {}
virtual int Area();
}

int main() {
Point2D p;
memset(&p, 0, sizeof(Point2D));
}
  1. 请你指出这段代码的严重错误。

3. 循环队列

class CircularQueue {
int * _data;
int _capacity;
int _head;
int _tail;
public:
bool isEmpty() {
return _head == _tail;
}
bool isFull() {
return (_tail + 1) % (_capacity+1) == _head;
}
  1. 为什么要%( _capacity+1)
  2. 如果_capacity = 8,_head = 2, _tail = 6, 请画出内存布局,并指出还有几个可用槽位。

4.

请你设计游戏位置信息的传输协议,每秒同步 20 次,共 5000 用户,总的上传带宽为 1MB/s

5.

Point rand(Point A, Point B, Point C) {
float u = rand(), v = rand();
if(u+v>1){u = 1-u; v = 1-v;}
return (
A.x*(1-u-v) + B.x*u + C.x*v,
A.y*(1-u-v) + B.y*u + C.y*v
)
}
  1. 为什么要判断 u+v>1
  2. 请你证明采样分布是均匀的。

6.

给你一个有序数组,请你去重,要求空间复杂度为 O(1),同时指出时间复杂度。

7.

忘记了。

8.

std::vector<int> arr;

void ThreadA() {
while(true) {
arr.push_back(1);
}
}

void ThreadB() {
while(true) {
if(!arr.empty()) {
arr.pop_back();
}
}
}
  1. 这段代码会有什么问题?

9.

二维平面上有一群敌人,你是一个摄像头,只能看到 90 度的范围,即[-45 度,45 度]。

  1. 在视野内的敌人+1 分,否则 0 分
  2. 距离 240 的敌人+1 分,否则 0 分

请问摄像头指向那里分最高。