<string> 结构中的矢量无法正常工作

vector<string> in a struct doesn't work properly

本文关键字:常工作 工作 string lt gt 结构      更新时间:2023-10-16

我声明了一个vector<string>,但我甚至无法编译它。我尝试了很多方法,但都不起作用。我正试图写出x.surface.push_back(word([I],但它显然写错了,我不知道如何正确地编写它并使其能够编译。

#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int number, i = 0;
string word;
struct donators {
vector<string> surname;
vector<int> amount;
} x;
cout << "How many donators do you want to register? " << endl;
cin >> number;
for (i = 0; i < number; i++) {
cout << "Surname: ";
cin >> word;
x.surname.push_back(word)[i];
cout << "Amount: ";
x.amount.push_back(i);
cin >> x.amount[i];
}
cout << "OUR GORGEUS DONATORS: " << endl;
for (i = 0; i < number; i++) {
if (x.amount[i] >= 10000) {
cout << "Surname: " << x.surname(word)[i];
cout << "Amount: " << x.amount[i] << endl;
}
else if (x.amount[i] < 10000) {
cout << "Lack of surnames!" << endl;
}
}
cout << "OUR CASUAL DONATORS: " << endl;
for (i = 0; i < number; i++) {
if (x.amount[i] < 10000) {
cout << "Surname: " << x.surname(word)[i];
cout << "Amount: " << x.amount[i] << endl;
} else if (x.amount[i] >= 10000) {
cout << "Lack of surnames!" << endl;
}
}
return 0;
}

还有一件事。如何使句子"缺少姓氏!"写一次?在某些情况下,它会被写两次或多次多余的内容。

您将[i]放在代码中看似随机的位置。例如在CCD_ 3中。如果你不确定他们在做什么,就不要在代码中添加这样的东西。

x.surname(word)[i]结构也是错误的。x.surname(word)应该是什么?此语法用于函数调用。然而,surname不是一个函数。这是std::vector<std::string>。只需放入x.surname[i]即可。

还有一件事。如何使句子"缺少姓氏!"写过一次吗?在某些情况下,它会被写两次或多次什么是多余的。

这是因为你为每个不符合标准的捐赠者写的。相反,要跟踪是否有捐赠者符合标准,只有在没有捐赠者符合标准时才打印。你可以这样做:

bool HasGorgeousDonators = false;

然后在循环中:

if (x.amount[i] >= 10000)
{
cout << "Surname: " << x.surname[i];
cout << "Amount: " << x.amount[i] << endl;
HasGorgeousDonators = true;
}

循环后:

if (!HasGorgeousDonators)
cout << "Lack of surnames!" << endl;

另一个循环也是如此。此外,请考虑以下问题;A:

为什么";使用命名空间std"被认为是不好的做法?

您似乎在用一些C++帮助函数编写C。然而,C++是一种不同的语言。当然,它支持一些C结构,但还有更多。看看我的一些实现建议,并将其与您的代码进行比较:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
template<typename T>
T ReadCin(std::string_view const& sv = "") {
T retVal;
if (!sv.empty()) std::cout << sv;
std::cin >> retVal;
return retVal;
}
class Donator {
private:
std::string surname{};
int amount{};
public:
constexpr bool IsGenerous() const noexcept { return amount >= 10000; }
void Read() noexcept {
surname = ReadCin<decltype(surname)>("Surname: ");
amount = ReadCin<decltype(amount)>("Amount: ");
}
friend std::ostream& operator<<(std::ostream& out, Donator const& donator) noexcept {
out << "Surname: " << donator.surname << ", " << "Amount: " << donator.amount;
return out;
}
};
int main() {
std::vector<Donator> donators(ReadCin<int>("How many donators do you want to register?n"));
for (auto& donator : donators) donator.Read();
std::cout << "OUR GENEROUS DONATORS:n";
std::copy_if(std::cbegin(donators), std::cend(donators), std::ostream_iterator<Donator>(std::cout, "n"),
[](Donator const& donator) { return donator.IsGenerous(); });
std::cout << "OUR CASUAL DONATORS:n";
for (auto const& donator : donators) if (!donator.IsGenerous()) std::cout << donator << 'n'; //alternative
}

我尝试使用C++来包含一些可能性。我真的建议你买一本关于C++的好书。