https://github.com/eldment/lin-q
📈 为 C++ 打造的高性能、纯头文件 LINQ 风格查询库 📦 High-performance, header-only, LINQ-style query library for C++
https://github.com/eldment/lin-q
cpp cpp17 header-only high-performance library linq linq-style mordern-cpp powerful
Last synced: 9 months ago
JSON representation
📈 为 C++ 打造的高性能、纯头文件 LINQ 风格查询库 📦 High-performance, header-only, LINQ-style query library for C++
- Host: GitHub
- URL: https://github.com/eldment/lin-q
- Owner: ELDment
- License: gpl-3.0
- Created: 2025-08-18T13:20:27.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-08-18T13:28:45.000Z (10 months ago)
- Last Synced: 2025-08-18T15:28:55.950Z (10 months ago)
- Topics: cpp, cpp17, header-only, high-performance, library, linq, linq-style, mordern-cpp, powerful
- Language: C++
- Homepage: https://github.com/ELDment/Lin-Q/blob/main/src/LinQ.hpp
- Size: 20.5 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lin-Q
**English** | [简体中文](README.zh-CN.md)
A simple, header-only, easy-to-use, and high-performance LINQ-like query library for C++ 🚀
This library provides a powerful set of query operations for collections, inspired by .NET's Language Integrated Query (LINQ)
## ✨ Features
- **📑 Header-only**: Just include `LinQ.hpp` to get started
- **⌛ Deferred Execution**: Operations like `Where` and `Select` are deferred until the results are actually needed
- **🎩 Powerful API**:
- Filtering: `Where`
- Projection: `Select`
- Sorting: `OrderBy`, `OrderByDescending`, `ThenBy`
- Aggregation: `Sum`, `Count`, `Average`, `Min`, `Max`
- Element: `First`, `FirstOrDefault`, `Last`, `LastOrDefault`, `ElementAt`
- Quantifiers: `Any`, `All`
- Set: `Distinct`, `Union`, `Intersect`, `Except`
- Conversion: `ToVector`, `ToMap`, `ToUnorderedMap`, `ToArray`, `ToCArray`
- ...
## 🛠️ Building the Project
### Using xmake
```bash
xmake
xmake run example
```
### Using CMake
```bash
mkdir build
cd build
cmake ..
cmake --build .
# Run the executable
./Release/example.exe
```
## 🚀 Usage Example
```cpp
#include "LinQ.hpp"
#include
#include
#include
int main() {
// Test with an integer array
int intArray[] = {1, 2, 3, 4, 5};
auto intQuery = LinQ::From(intArray)
.Select([](int x) { return x * x; });
for (auto x : intQuery) {
std::cout << x << " "; // Output: 1 4 9 16 25
}
std::cout << std::endl;
// Test with a vector of strings
std::vector stringVector = {"apple", "banana", "cherry", "date", "fig"};
auto stringQuery = LinQ::From(stringVector)
.Where([](const std::string& s) { return s.length() > 4; })
.Select([](const std::string& s) {
std::string upperString = s;
std::transform(upperString.begin(), upperString.end(), upperString.begin(), ::toupper);
return upperString;
})
.OrderByDescending([](const std::string& s) { return s; });
for (const auto& s : stringQuery) {
std::cout << s << " "; // Output: CHERRY BANANA APPLE
}
std::cout << std::endl;
// Test ToMap with a single argument
struct Person {
int id;
std::string name;
};
std::vector people = {{1, "ELDment"}, {2, "Ambr0se"}, {3, "利世"}};
auto personMap = LinQ::From(people).ToMap([](const Person& p) { return p.id; });
for (const auto& pair : personMap) {
std::cout << "[" << pair.first << ": " << pair.second.name << "] "; // Output: [1: ELDment] [2: Ambr0se] [3: 利世]
}
std::cout << std::endl;
return 0;
}
```
## 🤝 Contributing
Contributions, issues, and feature requests are welcome!