cmake_minimum_required(VERSION 3.5) project(Test) add_executable( te test.cpp )
test.cpp
1 #include <stdlib.h> 2 #include <iostream> 3 #include <stdio.h> 4 #include <fstream> 5 using namespace std; 6 int main() 7 { 8 int test[10] = {0}; // 全都赋值为0 9 for(int i = 0; i < 10; i++) 10 { 11 cout << test[i] << endl; 12 test[i]++; 13 14 } 15 std::cout << "=======================" << std::endl; 16 for (int j = 0; j < 10; j++) 17 { 18 std::cout << test[j] << std::endl; 19 } 20 return 0; 21 }
执行结果:
0 0 0 0 0 0 0 0 0 0 ======================= 1 1 1 1 1 1 1 1 1 1
sort
sort.cpp
1 #include <iostream> 2 #include <array> 3 #include <algorithm> 4 #include "opencv2/imgproc/imgproc.hpp" 5 #include "opencv2/highgui/highgui.hpp" 6 7 using namespace std; 8 9 struct Line{ // 大写 10 cv::Point2i p1, p2; 11 }; 12 13 bool compare(int a, int b) 14 { 15 return a < b; 16 } 17 18 bool cpr(Line l1, Line l2){ 19 return l1.p2.y > l2.p2.y; 20 } 21 22 23 int main() { 24 std::array<int, 10> s = { 5, 7, 4, 2, 8, 6, 1, 9, 0, 3 }; 25 26 sort(s.begin(), s.end(), compare); 27 for (auto a : s) { 28 std::cout << a << " "; 29 } 30 std::cout << ' ' << ' '; 31 32 vector<Line> lines; 33 Line l1, l2; 34 l1.p1 = cv::Point2i(1, 2); 35 l1.p2 = cv::Point2i(7, 3); 36 37 lines.push_back(l1); 38 39 l2.p1 = cv::Point2i(3, 10); 40 l2.p2 = cv::Point2i(8, 9); 41 lines.push_back(l2); 42 cout << "排序前:" << endl; 43 for (auto & l : lines){ 44 cout << l.p1.x << " " << l.p1.y << " " << l.p2.x << " " << l.p2.y << endl; 45 } 46 47 sort(lines.begin(), lines.end(), cpr); 48 cout << "排序后:" << endl; 49 for (auto & l : lines){ 50 cout << l.p1.x << " " << l.p1.y << " " << l.p2.x << " " << l.p2.y << endl; 51 } 52 53 return 0; 54 }
CMakeLists.txt
cmake_minimum_required(VERSION 3.9) project(sort) set(CMAKE_CXX_STANDARD 11) FIND_PACKAGE(OpenCV REQUIRED) add_executable(sort main.cpp) TARGET_LINK_LIBRARIES(sort ${OpenCV_LIBS})
执行结果:
0 1 2 3 4 5 6 7 8 9 排序前: 1 2 7 3 3 10 8 9 排序后: 3 10 8 9 1 2 7 3 Process finished with exit code 0