gpt4 book ai didi

c++ vector 迭代器输出在列中垂直显示

转载 作者:行者123 更新时间:2023-11-30 04:57:46 24 4
gpt4 key购买 nike

我正在尝试使用 vector 迭代器按列输出我的字符串,如附图所示。现在我只是展示一个使用名称和苹果酒顺序的测试用例,之后我想为其他人复制相同的。 Desired output screenshot

下面是我当前的代码:

// values for controlling format
const int name_width = 15 ;
const int int_width = 7 ;
const int dbl_width = 12 ;
const int num_flds = 7 ;
const std::string sep = " |" ;
auto total_width = name_width*2 + int_width*2 + dbl_width*3 + sep.size() * num_flds ;
const std::string line = sep + std::string( total_width-1, '-' ) + '|' ;

cout<<"How many people ordered? ";
cin>>odrs; // Store number of orders

for(int i=1; i<=odrs; i++){
cout<<"Enter the name of person #"<<i<<":"<<endl;;
cin>>names; // Store names of users

odrNames.push_back(names); // Store names as entered as

cout<<"How many orders of cider did "<<names<<" have? ";
cout<<endl;
cin>>odrciderjuice; // Store Cider order item
sbCider = odrciderjuice * 5.5; // Calculate Cider order per price
odrCider.push_back(odrciderjuice); // Store Cider order item based on entry
SbCider.push_back(sbCider); // Store calculated Cider order per price

cout<<"How many orders of apple juice did "<<names<<" have? ";
cout<<endl;
cin>>odrapplejuice; // Store Juice order item
sbJuice = odrapplejuice * 4.5; // Calculate Juice order per price
odrApple.push_back(odrapplejuice); // Store Juice order item based on entry
SbJuice.push_back(sbJuice); // Store calculated Juice order per price
cout<<endl;

total = sbCider + sbJuice; // Calculate total between Cider and Juice
GTotal.push_back(total); // Store total values after calculation
cout<<endl;
}

for(vector<string>::iterator naming = odrNames.begin(); naming!= odrNames.end(); ++naming)
cout << sep << std::setw(name_width) << *naming<<"\v";

for(vector<int>::iterator ciderOdr = odrCider.begin(); ciderOdr!= odrCider.end(); ++ciderOdr)
cout <<*ciderOdr;

最佳答案

使用迭代器确实是浏览集合的惯用方式。简单地说,您需要一个对象集合,而不是一堆不相关的集合!

此外,你的名字应该更加一致,只在单个值和它的集合之间使用大写的名字是 future 维护的噩梦......

C++ 是一种 OO 语言,而 OO 编程是一种将复杂程序拆分为较小单元(类)的方法,这些单元仅负责较大程序的一小部分。这旨在生成更易于测试和维护的代码。

这里是 Order类可以包含:

  • 客户名称的一个字符串
  • 2 个整数表示苹果酒和果汁的数量
  • 可选的计算详细价格和总价的方法

由于您的格式设置并不简单,我会使用替代类来处理它。该类将包含:

  • vector<Order> 的引用
  • ostream 的引用
  • 格式细节的静态元素
  • 显示标题行、水平虚线、订单行以及总计和平均线的方法。

然后 main 将只包含加载订单的代码。

代码可以是:

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

struct Order {
static constexpr double ciderPrice = 5.5;
static constexpr double juicePrice = 4.5;
std::string name;
int cider;
int juice;

double sbCider() const {
return cider * ciderPrice;
}
double sbJuice() const {
return juice * juicePrice;
}
double total() const {
return sbCider() + sbJuice();
}
};

class Bill {
// values for controlling format
static const int name_width = 10 ;
static const int int_width = 6 ;
static const int dbl_width = 17 ;

const std::vector<Order>& orders;
std::ostream& out;

...

public:
Bill(const std::vector<Order>& orders, std::ostream& out):orders(orders),out(out) { }
Bill& show_line() {
...
return *this;
}
Bill& show_header() {
...
return *this;
}
Bill& show_order(const Order &order) {
...
return *this
}
Bill& show_total() {
return *this
}
Bill &show_average() {
...
return *this;
}
Bill& show() {
...
show_header().show_line();
for (const Order& order: orders) {
show_order(order);
}
return show_line().show_total().show_average().show_line();
}
};
int main() {
Order order;
vector<Order> orders;

int odrs;

cout<<"How many people ordered? ";
cin>>odrs; // Store number of orders

for(int i=1; i<=odrs; i++){
cout<<"Enter the name of person #"<<i<<":"<<endl;;
cin>>order.name; // Store names of users

cout<<"How many orders of cider did "<<order.name<<" have? "<<endl;
cin>>order.cider; // Store Cider order item

cout<<"How many orders of apple juice did "<<order.name<<" have? "<<endl;
cin>>order.juice; // Store Juice order item
orders.push_back(order);
}

Bill bill(orders, std::cout);
bill.show();
return 0;
}

关于c++ vector 迭代器输出在列中垂直显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51958544/

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