动态分配no。的值来配对对象
答
实例化的std ::对,你可以使用:
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型的载体,用价值100我让你猜你能做什么?)
std::vector<int> third (second.begin(),second.end());
这一个迭代另一个向量。有创意,不要犹豫,看看文档! (还有,请检查operator new
的文档如果要动态地创建它,你会需要它:))。
答
您可以使用此:
#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;
}
通过任何可能的手段。例如,使用'std :: vector'或operator'new'。 – Ari0nhh
['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)和['std :: pair'](http://en.cppreference.com/w/) cpp/utility/pair)或许? –
我的意思是如何使用std :: pair输入多组值? –