To store a set of coordinates in C++, you can use a vector of pairs. Each pair represents an (x, y) coordinate. Here’s an example:
cpp#include
#include
using namespace std;
int main() {
vectorint, int>> coordinates;
coordinates.push_back({0, 0});
coordinates.push_back({0, 1});
coordinates.push_back({1, 1});
// Accessing coordinates
for (const auto& coord : coordinates) {
cout << "(" << coord.first << ", " << coord.second << ")" << endl;
}
return 0;
}
In this code, vector
creates a vector of pairs to store the coordinates. coordinates.push_back({x, y});
adds a new coordinate to the vector.
This approach is more efficient and easier to work with than using strings to represent coordinates.
المزيد من المعلومات
يمكنك أيضًا استخدام هيكل بيانات يحتوي على مصفوفة ثنائية الأبعاد لتخزين الإحداثيات، حيث يتم استخدام كل صف في المصفوفة لتخزين إحداثيات نقطة معينة. هنا مثال على كيفية فعل ذلك:
cpp#include
#include
using namespace std;
int main() {
vectorint>> coordinates;
coordinates.push_back({0, 0});
coordinates.push_back({0, 1});
coordinates.push_back({1, 1});
// Accessing coordinates
for (const auto& coord : coordinates) {
cout << "(" << coord[0] << ", " << coord[1] << ")" << endl;
}
return 0;
}
في هذا المثال، تم إنشاء vector
لتخزين الإحداثيات. ويتم إضافة كل إحداثيات جديدة باستخدام coordinates.push_back({x, y});
.
استخدام مصفوفة ثنائية الأبعاد يمكن أن يكون مفيدًا إذا كانت لديك حالات استخدام تتطلب تخزين الإحداثيات كمجموعات من البيانات الأخرى.