C++ tricks For Reference

std::bind

#include <functional>
#include <iostream>
#include <string>
void func(int i, char c, int j, std::string s, int k) {
  std::cout << i << " " << c << " " << j << " " << s << " " << k << std::endl;
}
int main() {
  auto f = std::bind(func, 1, 'c', std::placeholders::_1, std::string("hello"),
                     std::placeholders::_2);
  f(10, 20);
  f(101, 50);
  return 0;
}
/* 输出:
1 c 10 hello 20
1 c 101 hello 50

abi::__cxa_demangle 返回类的名字字符串

下面这个Point一个默认构造的实现参考了下 protobuf 的 pb.cc

#include <cxxabi.h>
#include <iostream>
#include <typeinfo>
class Point {
 public:
  Point() {
    std::cout << "Point()" << std::endl;
    memset(&x, 0, static_cast<size_t>(reinterpret_cast<char*>(&y) -
                                      reinterpret_cast<char*>(&x) + sizeof(y)));
  }
  Point(int _x = 0, int _y = 0) : x(_x), y(_y) {
    std::cout << "Point(int _x = 0, int _y = 0)" << std::endl;
  }
  ~Point() { std::cout << "~Point()" << std::endl; }

  void print() { std::cout << " (" << x << " , " << y << ") " << std::endl; }
  int x;
  int y;

  std::string GetTypeName() {
    return abi::__cxa_demangle(typeid(*this).name(), 0, 0, 0);
  }
};

文件读写

 1. 判断文件是否存在

int main(){
  std::string name="az.txt";
  std::ifstream f(name.c_str());
  if(f.good()){
    std::cout<<"yes"<<std::endl;
  }
  else{
    std::cout<<"no"<<std::endl;
  }
  f.close();

  return 0;
}
int main()
{

  std::ofstream ofs;
  ofs.open("test.txt",std::ios::trunc);

  for(int i=0;i<=20;++i)
  {
    ofs<<i<<": there\n";
  }
  ofs.close();

  return 0;
}

2.  逐行读取文件

int main()
{
  std::string line;
  std::ifstream ifs;
  ifs.open("a.txt",std::ios::in);
  if(ifs.fail()){
    std::cout<<"fail to open fail a.txt"<<std::endl;
  }else{
    while (getline(ifs,line))
    {
       std::cout<<"line: "<<line<<std::endl;
      std::cout<<"line.size: "<<line.size()<<std::endl;
    }
  }
  ifs.close();

  return 0;
}

3. 创建文件并写入

int main()
{

  std::ofstream ofs;
  ofs.open("test.txt",std::ios::trunc);

  for(int i=0;i<=20;++i)
  {
    ofs<<i<<": there\n";
  }
  ofs.close();
  return 0;
}

字符流


  std::string str;
  uint8_t arr[3]={'a','c','z'};
  for (int i = 0; i < 3; ++i) {
    std::cout<<i<<": "<<arr[i]<<std::endl;
    std::ostringstream os;
    os << arr[i];
    str += os.str();
  }
  std::cout<<"str: "<<str<<">"<<std::endl;

优先级队列

priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction.A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the top().

优先级队列是容器适配器,可常数时间内查询最大元素(默认),当然用户提供Compare改变其顺序,如使用 std::greator<T> 可以让top() 返回最小值。

leetcode 给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。

class Solution {
public:
    //小顶堆
    class  comp{
        public:
        bool operator()(const std::pair<int,int>& lhs,const std::pair<int,int>& rhs){
            return lhs.second>rhs.second;
        }
    };
    vector<int> topKFrequent(vector<int>& nums, int k) {
        std::unordered_map<int,int> mps;
        for(const auto  elem : nums){
            ++mps[elem];
        }

        std::priority_queue<std::pair<int,int>,std::vector<std::pair<int,int>>,comp> priq;
        for(const auto elem:mps){
            priq.push(elem);
            if(priq.size()>k) priq.pop();
        }

        std::vector<int> res;
        res.resize(k);
        for(int i = k-1;i>=0;--i){
            res[i] = priq.top().first;
            priq.pop();
        }

        return res;
    }
};

基于比较运算符重载的优先级队列

#include <fstream>
#include <iostream>
#include <queue>
#include <vector>

struct Node {
  int size;
  int price;
  bool operator<(const Node &b) const {
    return this->size == b.size ? this->price > b.price : this->size < b.size;
  }
};
class Cmp {
public:
  bool operator()(const Node &a, const Node &b) {
    return a.size == b.size ? a.price > b.price : a.size < b.size;
  }
};

int main() {
  std::priority_queue<Node, std::vector<Node>> priorityQueue;
  for (int i = 0; i < 5; i++) {
    priorityQueue.push(Node{i, 5 - i});
  }
  priorityQueue.push(Node{4, 0});

  while (!priorityQueue.empty()) {
    Node top = priorityQueue.top();
    std::cout << "size:" << top.size << " price:" << top.price << std::endl;
    priorityQueue.pop();
  }
  return 0;
}

/* 输出size的递减序列,size相同的price递增序列
size:4 price:0
size:4 price:1
size:3 price:2
size:2 price:3
size:1 price:4
size:0 price:5

如果改成 this->size > b.size; 输出为递增序列
size:0 price:5
size:1 price:4
size:2 price:3
size:3 price:2
size:4 price:0
size:4 price:1

基于函数对象的优先级队列:

#include <fstream>
#include <iostream>
#include <queue>
#include <vector>

struct Node {
  int size;
  int price;
};
class Cmp {
public:
  bool operator()(const Node &a, const Node &b) {
    return a.size == b.size ? a.price > b.price : a.size < b.size;
  }
};

int main() {
  std::priority_queue<Node, std::vector<Node>, Cmp> priorityQueue;
  for (int i = 0; i < 5; i++) {
    priorityQueue.push(Node{i, 5 - i});
  }
  priorityQueue.push(Node{4, 0});

  while (!priorityQueue.empty()) {
    Node top = priorityQueue.top();
    std::cout << "size:" << top.size << " price:" << top.price << std::endl;
    priorityQueue.pop();
  }
  return 0;
}
/* 
size:4 price:0
size:4 price:1
size:3 price:2
size:2 price:3
size:1 price:4
size:0 price:5

二进制的读写

向文件写入

int main(int argc, char *argv[]) {
  std::string filename = argv[1];

  std::ofstream file(filename, std::ios::binary | std::ios::out);

  std::bitset<8> num; //(std::string("00000110"));
  num = 0b00000111;
  file << num << std::endl;
  file.close();

  return 0;
}

/* 
执行 ./a.out z.txt

打开 z.txt 可以看到文件内有 00000111

从文件读取

int main(int argc, char *argv[]) {
  std::string filename = argv[1];

  std::ifstream ifile(filename, std::ios::binary | std::ios::in);
  std::string line;
  std::getline(ifile, line);
  std::cout << "line: " << line << std::endl;

  std::bitset<8> bits(line);
  std::cout << bits.to_ulong() << std::endl;

  return 0;
}

/*
执行./a.out z.txt
打印
line: 00000111
7

pb 有提供二进制写入的接口

bool SerializePartialToOstream(std::ostream* output) const;

字符串与整型数相互转换

#include <iostream>  
#include <string>  
  
int main() {  
    std::string str = "123456789012345";  
    unsigned long long num;  
  
    try {  
        num = std::stoull(str);  
        std::cout << "转换成功: " << num << std::endl;  
    } catch (const std::invalid_argument& e) {  
        std::cerr << "无效参数: " << e.what() << std::endl;  
    } catch (const std::out_of_range& e) {  
        std::cerr << "超出范围: " << e.what() << std::endl;  
    }  
  
    return 0;  
}

### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况时,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可证服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为或完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值