我正在尝试确定用户输入有序对的域和范围
I'm trying to determine the domain and range of user input ordered pairs
我让用户在一个成对的向量中输入。我正试图通过删除重复的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;
}
如果您只需要x
和y
的唯一值(不必担心它们之间的关系),那么您应该能够使用几个集合(std::set
)。
只需创建两个空集,xset
和yset
,然后遍历原始向量中的所有坐标。
对于每个坐标,将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";
}
的输出显示了信息如何流入集合(删除重复项)并返回到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
的值视为完全独立的如果您不想考虑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
相关文章:
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- 从用户输入范围内输出质量数量
- 我正在尝试创建一个程序,用户在其中输入一系列数字,并且我想列出范围之间的质数
- 循环直到整数输入在所需范围内无法处理非数字字符输入
- 基于范围的输入流循环
- 扩展编译器以在编译时计算"complicated"函数(具有已知的输入值),超出"constexpr"的范围
- <algorithm>在同一输入迭代器范围内并排运行两个
- 我正在尝试确定用户输入有序对的域和范围
- 在此范围内未声明"输入"
- 错误输入未在此范围中显示
- 排序效率 算法与输入范围相关
- 只允许在QDoubleValidator的范围内输入QLineEdit
- 无法正确验证无效输入并接受带有C++的正确范围值
- 在C++中实现随机预言机(输出在范围内是随机的,但对于相同的输入输出是相同的)
- 检查输入是否在其C++限制范围内