我想存储图表的坐标数,需要成对分配。如何成对动态分配'n'个坐标?
答案 0 :(得分:0)
你可以用这个:
#include <iostream>
using namespace std;
#include <vector>
void doSomething(){
int x = 1, y= 3;
// vector of the graph's points
vector<pair<int, int>> graph;
// add point to the vector
graph.push_back(make_pair(x, y));
// accessing a point // here accessing first point at index 0
// you can loop through the vector when having many points
cout<<"x = "<< graph.at(0).first <<endl;
cout<<"y = "<< graph.at(0).second <<endl;
}
答案 1 :(得分:-1)
要实例化std :: pair,您可以使用:
std::pair<int, double> p2(42, 0.123);
std::cout << "Initialized with two values: "
<< p2.first << ", " << p2.second << '\n';
对于载体:
std::vector<int> second (4,100);
(此行创建一个4 int的Vector,值为100.我猜你猜怎么办?)
std::vector<int> third (second.begin(),second.end());
这个迭代另一个向量。要有创意,不要犹豫,看看文档!
(另外,请查看operator new
的文档。如果要动态创建它,则需要它:))