我正在尝试确定用户输入有序对的域和范围

I'm trying to determine the domain and range of user input ordered pairs

本文关键字:范围 输入 用户      更新时间:2023-10-16

我让用户在一个成对的向量中输入。我正试图通过删除重复的x值来确定域。然后我需要通过y的所有唯一值来确定范围。我已经查看了地图。我好像想不通。这是家庭作业,所以我更愿意了解如何做,而不仅仅是一个解决方案。谢谢

代码:

#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;
}

如果您只需要xy的唯一值(不必担心它们之间的关系),那么您应该能够使用几个集合(std::set)。

只需创建两个空集,xsetyset,然后遍历原始向量中的所有坐标。

对于每个坐标,将x值添加到xset,并将y值添加到yset

然后,当你完成时,你在两个集合中都有所有唯一的值,你可以从每个集合中提取最小和最大(或所有值),以给出范围和域。

下面的代码(不要在作业中逐字逐句地使用,你几乎肯定会因为抄袭而被ping)展示了如何做到这一点:

#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";
}

的输出显示了信息如何流入集合(删除重复项)并返回到xy值的各个向量中:

input data:
   (1,2)
   (9,99)
   (314158,271828)
   (1,77)
   (1,99)
x values:
   1
   9
   314158
y values:
   2
   77
   99
   271828

请记住,这会将xy的值视为完全独立的如果您不想考虑x已经存在的坐标的y值,这是一个轻微的修改:

// 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