使用 llvm/ADT/STLExtras.h 扩展STL 标准库操作
STLExtras.h 文件中给出了很多STL标准中很多函数的扩展。例如我们可以同时遍历两个容器。
#include <vector>
#include <string>
#include <iostream>
#include "llvm/ADT/STLExtras.h"
int main() {
std::vector<int> numbers = {1, 2, 3, 4};
std::vector<std::string> names = {"one", "two", "three", "four"};
// 使用 llvm::zip 来同时遍历两个向量
for (auto&& [num, name] : llvm::zip(numbers, names)) {
std::cout << num << " : " << name << std::endl;
}
return 0;
}
上述代码中给出了利用llvm::zip 对number 和 names 两个结构同时遍历的操作。
在macOS 下可以很方的编译输出结果。
brew install llvm clang
brew install llvm clang
clang++ -o main main.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core`
执行结果如下
更多LLVM 相关内容请持续关注