C style in C++ API
回答済みHi,
I’ve noticed the c++ exemples use c style arrays to process the data, e.g. sub tour elimination in tsp-c++. Is there a reason for that and would it be a drawback to use the stl with the c++ api?
Best regards
-
Hi Dominik,
The use of C-style arrays in Gurobi's C++ examples, particularly in the TSP example, can be attributed to several factors:
- Historical Context: Many of the examples were likely developed when C-style arrays were more commonly used, and they've been maintained for consistency and compatibility.
- Direct Memory Layout: C-style arrays provide a contiguous memory layout that can be directly passed to the Gurobi C++ API functions that expect pointer arguments. This is particularly useful when interfacing with the underlying optimization engine.
- Simplicity: For simple examples, C-style arrays can provide a straightforward implementation, especially when dealing with fixed-size data structures.
However, using STL containers (like
std::vector,std::array) with the Gurobi C++ API is perfectly acceptable and often recommended for modern C++ applications for several reasons:-
Safety and Modern C++ Features:
- STL containers provide bounds checking (in debug mode)
- Automatic memory management
- Better exception safety
- Integration with modern C++ algorithms and features
- Flexibility: STL containers can be easily resized and modified, which is particularly useful when dealing with dynamic problem sizes.
- Performance: Modern STL implementations are highly optimized, and the performance difference between STL containers and C-style arrays is typically negligible in most applications.
The only potential drawback might be when interfacing with Gurobi functions that expect raw pointers - you'll need to use
.data()to get the underlying array, but this is a minor inconvenience compared to the benefits of using STL containers.- Bot
0
サインインしてコメントを残してください。
コメント
1件のコメント