<string> 结构中的矢量无法正常工作
vector<string> in a struct doesn't work properly
我声明了一个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++的好书。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程