gpt4 book ai didi

c++ - 问:如何创建有序的字符串对

转载 作者:行者123 更新时间:2023-12-02 10:11:05 27 4
gpt4 key购买 nike

我想要一些有关当前正在使用的程序的指导,如果用户两次输入相同的值,我已经成功创建了一对整数并处理了异常。我一直坚持执行创建对的相同过程,但不是整数,它必须是字符串。
我的一个建议是更改有序对的随机生成,方法是:

string empty = "";
myList2[i].setFirst(empty + char('a' + rand() % 26));
myList2[i].setSecond(empty + char('A' + rand() % 26));
朝正确方向提出的任何建议将不胜感激。
客户端文件:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <exception>
#include "orderedpair.h"
using namespace std;
using namespace cs_pairs;

int main() {

int num1, num2;
OrderedPair<int> myList[10];

srand(static_cast<unsigned>(time(0)));
cout << "default value: ";
myList[0].print();
cout << endl;

for (int i = 0; i < 10; i++) {
myList[i].setFirst(rand() % 50);
myList[i].setSecond(rand() % 50 + 50);
}

myList[2] = myList[0] + myList[1];

if (myList[0] < myList[1]) {
myList[0].print();
cout << " is less than ";
myList[1].print();
cout << endl;
}

for (int i = 0; i < 10; i++) {
myList[i].print();
cout << endl;
}

cout << "Enter two numbers to use in an OrderedPair. Make sure they are different numbers: ";
cin >> num1 >> num2;
OrderedPair<int> x;

/* use this before you've implemented the exception handling in the class:
x.setFirst(num1);
x.setSecond(num2);
*/

/* use this after you've implemented the exception handling in the class: */
try {
x.setFirst(num1);
x.setSecond(num2);

/*if (x.setFirst(num1) == x.setSecond(num2))
{
throw OrderedPair<int>::DuplicateMemberError;
} */

}
catch (OrderedPair<int>::DuplicateMemberError e) {
cout << "YOU ENTERED TWO OF THE SAME VALUES!" << endl;
x.setFirst(OrderedPair<int>::DEFAULT_VALUE);
x.setSecond(OrderedPair<int>::DEFAULT_VALUE);
}

cout << "The resulting OrderedPair: ";
x.print();
cout << endl;
}
头文件:
#ifndef ORDEREDPAIR_H
#define ORDEREDPAIR_H


#include <iostream>

namespace cs_pairs
{
template <class T>
class OrderedPair
{
public:
typedef std::size_t size_type;
typedef T value_type; // changed int to T
static const int DEFAULT_VALUE = 0;

class DuplicateMemberError
{

};

OrderedPair(T newFirst = DEFAULT_VALUE, T newSecond = DEFAULT_VALUE);

void setFirst(T newFirst);

void setSecond(T newSecond);

T getFirst() const;
T getSecond() const;

OrderedPair<T> operator+(const OrderedPair<T>& right) const;

bool operator<(const OrderedPair<T>& right) const;

void print() const;

private:
T first;
T second;
};
}
#include "orderedpair.cpp"
#endif // !ORDEREDPAIR_H
实现文件:
#include <iostream>
#include <exception>
using namespace std;

namespace cs_pairs
{
template <class T>
OrderedPair<T>::OrderedPair(T newFirst, T newSecond) {
setFirst(newFirst);
setSecond(newSecond);
}

template <class T>
void OrderedPair<T>::setFirst(T newFirst) {
if ((newFirst == second) && (newFirst != 0))
{
throw DuplicateMemberError();
}
first = newFirst;
}

template <class T>
void OrderedPair<T>::setSecond(T newSecond) {
if ((newSecond == first) && (newSecond != 0))
{
throw DuplicateMemberError();
}
second = newSecond;
}

template <class T>
T OrderedPair<T>::getFirst() const {
return first;
}

template <class T>
T OrderedPair<T>::getSecond() const {
return second;
}

template <class T>
OrderedPair<T> OrderedPair<T>::operator+(const OrderedPair<T>& right) const {
return OrderedPair(first + right.first, second + right.second);
}

template <class T>
bool OrderedPair<T>::operator<(const OrderedPair<T>& right) const {
return first + second < right.first + right.second;
}

template <class T>
void OrderedPair<T>::print() const {
std::cout << "(" << first << ", " << second << ")";
}
}

最佳答案

要通用,请摆脱static const int DEFAULT_VALUE = 0;摆脱不尊重您的不变性的默认构造函数
我会摆脱单独的二传手,当一起使用时可能会出现问题:

void swap_order(OrderedPair<int>& op)
{
const auto first = op.getFirst();
op.setFirst(op.getSecond()); // Oups... throw exception
op.setSecond(first);
}
所以:
template <class T>
class OrderedPair
{
public:
using size_type = std::size_t;
using value_type = T;

class DuplicateMemberError{};

OrderedPair(const T& newFirst, const T& newSecond) : first(newFirst), second(newSecond) { check(); }

const T& getFirst() const { return first; }
const T& getSecond() const { return second; }

OrderedPair<T> operator+(const OrderedPair<T>& right) const;
bool operator<(const OrderedPair<T>& rhs) const;
void print() const;

private:
void check() { if (first == second) throw DuplicateMemberError{}; }
private:
T first;
T second;
};
由于不再默认可构造,因此您必须进行更改 OrderedPair<int> myList[10];通过
std::vector<OrderedPair<int>> myList; /*myList.reserve(10);*/
一代将是:
//const std::string empty;
const char lower_alphabet = "abcdefghijklmnopqrstuvwxyz"; // 'a'-'z' is not guaranty to be contiguous (EBCDIC)
const char upper_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 'A'-'Z' is not guaranty to be contiguous (EBCDIC)
for (int i = 0; i < 10; i++) {
myList.emplace_back(rand() % 50, rand() % 50 + 50);
// myList.emplace_back(empty + char(lower_alphabet[rand() % 26], empty + char(upper_alphabet[rand() % 26]);
}

关于c++ - 问:如何创建有序的字符串对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63555028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com