我在一对矢量中输入了用户。我试图通过删除重复的x值来确定域。然后我需要通过y的所有唯一值来确定范围。我看了一下地图。我似乎无法弄明白。这是作业,所以我更愿意了解如何做而不仅仅是一个解决方案。谢谢!
CODE:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
vector<pair<int, int> > sample;
vector< vector<int> > relation;
vector< vector<int> > domain;
vector< vector<int> > range;
bool loop = true;
do {
int input;
int input2;
cout << "Enter the first INT in the ordered pair: ";
cin >> input;
cout << "Enter the second INT in the ordered pair: ";
cin >> input2;
if (input != -1 && input2 != -1) {
sample.push_back(make_pair(input, input2));
}
else if (input != -1 && input2 == -1) {
cout << "ERROR, must input 2 INTS for an ordered pair." << endl;
loop = false;
}
else if (input == -1) {
loop = false;
}
} while (loop == true);
map<int, int> s;
int size = sample.size();
for (unsigned i = 0; i < size; ++i) s.insert(sample[i]);
sample.assign(s.begin(), s.end());
cout << "TEST: { ";
for (int i = 0; i < s.size(); i++) {
cout << "(" << s[i].first << "," << s[i].second << ")" << ", ";
}
cout << "}" << endl;
cout << "Relation: { ";
for (int i = 0; i < sample.size(); i++) {
cout << "(" << sample[i].first << "," << sample[i].second << ")" << ", ";
}
cout << "}" << endl;
cout << "Domain: { ";
for (int i = 0; i < sample.size(); i++) {
cout << sample[i].first << ", ";
}
cout << "}" << endl;
cout << "Range: { ";
for (int i = 0; i < sample.size(); i++) {
cout << sample[i].second << ", ";
}
cout << "}" << endl;
return 0;
}
答案 0 :(得分:1)
如果您想要的只是x
和y
的唯一值(不必担心它们之间的关系),您应该只能使用几个集合(std::set
)。
只需创建两个空集xset
和yset
,然后运行原始向量中的所有坐标。
对于每个坐标,将x
值添加到xset
,将y
值添加到yset
。
然后,当你完成后,你拥有两个集合中的所有唯一值,你可以从每个集合中提取最小和最大(或所有值)来给出范围和域。
以下代码(不要在作业中逐字使用,你几乎肯定会被抄袭)这说明了如何做到这一点:
#include <iostream>
#include <utility>
#include <vector>
#include <set>
using namespace std;
typedef pair < int, int > tPII;
typedef vector < tPII > tVPII;
typedef vector < int > tVI;
typedef set < int > tSI;
int main() {
// Test data to use.
tVPII sample;
tPII a ( 1, 2); sample.push_back (a);
tPII b ( 9, 99); sample.push_back (b);
tPII c (314158, 271828); sample.push_back (c);
tPII d ( 1, 77); sample.push_back (d);
tPII e ( 1, 99); sample.push_back (e);
// Place individual coordinates into sets.
cout << "input data:\n";
tSI xset, yset;
for (tVPII::iterator it = sample.begin(); it != sample.end(); ++it) {
cout << " (" << it->first << "," << it->second << ")\n";
xset.insert (it->first);
yset.insert (it->second);
}
// Construct range/domain vectors from sets.
tVI xvals;
tVI yvals;
for (tSI::iterator it = xset.begin(); it != xset.end(); ++it)
xvals.push_back (*it);
for (tSI::iterator it = yset.begin(); it != yset.end(); ++it)
yvals.push_back (*it);
// Output range/domain vectors.
cout << "x values:\n";
for (tVI::iterator it = xvals.begin(); it != xvals.end(); ++it)
cout << " " << *it << "\n";
cout << "y values:\n";
for (tVI::iterator it = yvals.begin(); it != yvals.end(); ++it)
cout << " " << *it << "\n";
}
其输出显示信息如何流入集合(删除重复项)并返回到x
和y
值的各个向量:
input data:
(1,2)
(9,99)
(314158,271828)
(1,77)
(1,99)
x values:
1
9
314158
y values:
2
77
99
271828
请注意,这会将x
和y
值视为完全分开。 如果您不想将已存在y
的坐标的x
值纳入该帐户,则会稍作修改:
// Place individual coordinates into sets,
// discount coordinates totally if x has already been seen.
cout << "input data:\n";
tSI xset, yset;
for (tVPII::iterator it = sample.begin(); it != sample.end(); ++it) {
cout << " (" << it->first << "," << it->second << ") - ";
if (xset.find (it->first) != xset.end()) {
cout << "duplicate.\n";
} else {
cout << "inserting.\n";
xset.insert (it->first);
yset.insert (it->second);
}
}
的输出显示已更改的行为:
input data:
(1,2) - inserting.
(9,99) - inserting.
(314158,271828) - inserting.
(1,77) - duplicate.
(1,99) - duplicate.
x values:
1
9
314158
y values:
2
99
271828