vector容器存储的10个坐标拼接成带中括号的字符串坐标
时间: 2023-11-30 20:01:52 浏览: 170
存储过程拼接字符串
假设每个坐标是一个包含两个整数的pair类型,可以使用以下代码将10个坐标拼接成带中括号的字符串坐标:
```c++
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
vector<pair<int, int>> coordinates = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}, {15, 16}, {17, 18}, {19, 20}};
stringstream ss;
ss << "[";
for (int i = 0; i < coordinates.size(); i++) {
ss << "[" << coordinates[i].first << "," << coordinates[i].second << "]";
if (i != coordinates.size() - 1) {
ss << ",";
}
}
ss << "]";
string str_coordinates = ss.str();
cout << str_coordinates << endl;
return 0;
}
```
输出结果为:
```
[[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18],[19,20]]
```
阅读全文