如何打印出结构向量?它不保存数据吗?
Posted
技术标签:
【中文标题】如何打印出结构向量?它不保存数据吗?【英文标题】:How Do I Print Out a Vector of Structs? Is It Not Saving the Data? 【发布时间】:2013-12-03 16:34:45 【问题描述】:所以我到处寻找,但找不到任何可以帮助我解决问题的东西。
这就是我的 main 的样子(我为我的菜单取出了 switch 语句以节省空间)。
#include <iostream>
#include <iomanip>
#include <vector>
#include "functions.h"
using namespace std;
void addAProduct (vector<Product>);
int main()
cout << setprecision(2) << fixed << showpoint << left; //Format Output
char option; //Declare Variable
vector<Product> productVect; //Create Empty Vector of Structs
bool menu = true;
while (menu) //While Loop
cout << "\nMenu"; //Menu
cout << "\n1. Display Products";
cout << "\n2. Add a Product";
cout << "\n3. Edit a Product";
cout << "\n4. Exit";
cout << "\n\n";
cin >> option;
void addAProduct (vector<Product> vect)
Product tempProduct;
tempProduct.upc = 100000 + (rand() % 99999);
cin.ignore(256,'\n');
cout <<"\nPlease enter a name for the Product's Manufacturer. ";
getline(cin, tempProduct.manufacturer);
cout << "\nPlease enter a name for the Product. ";
getline(cin, tempProduct.name);
cout << "\nPlease enter a value for the Product's Price. ";
cin >> tempProduct.price;
cout << "\nPlease enter a value for the Product's Quantity. ";
cin >> tempProduct.quantity;
tempProduct.value = tempProduct.quantity * tempProduct.price;
vect.push_back(tempProduct);
我还在其中添加了一个函数,它最初来自我的函数头文件,但是当它不起作用时,我认为当我将它放入 main 时它会起作用。但是还是不行。
关于打印结构,这是我的函数头文件中的函数,我还添加了结构和原始添加产品功能。
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
struct Product //Struct of Product
int upc; //All Members to Be Determined By User at a Later Time with the Exception of the UPC
string manufacturer;
string name;
float price;
int quantity;
double value;
;
//Display Products
void displayProducts(vector<Product> vect)
if(vect.empty())
cout << "\nSorry, no values exist in the database.";
else
for (int count = 0; count < vect.size(); count++) //For Loop to Display All Products
cout << "\nUPC: " << vect[count].upc << "\nManufacturer: " << vect[count].manufacturer << "\nProduct Name: " << vect[count].name << "\nPrice: $" << vect[count].price << "\nQuantity: " << vect[count].quantity << "\nTotal Value: $" << vect[count].value << endl;
cout << endl;
//Add a Product
void addProduct(vector<Product> vect)
Product tempProduct;
tempProduct.upc = 100000 + (rand() % 99999);
cin.ignore(256, '\n');
cout << "\nPlease enter a name for the Product's manufacturer. ";
getline(cin, tempProduct.manufacturer);
cout << "\nPlease enter a name for the Product. ";
getline(cin, tempProduct.name);
cout << "\nPlease enter a value for the Product's Price. $";
cin >> tempProduct.price;
cout << "\nPlease enter a value for the Product's Quantity. ";
cin >> tempProduct.quantity;
tempProduct.value = tempProduct.quantity * tempProduct.price;
vect.push_back(tempProduct);
我没有编译器错误,但是当我尝试通过添加产品然后显示它来运行它时,它仍然输出没有值。
有什么想法吗?
【问题讨论】:
能否请您先更正您上面无法编译的代码? 它在 unix 服务器上为我编译... 【参考方案1】:通过参考:
void addAProduct (vector<Product>& vect)
//^
避免添加到副本中。发布的代码将元素添加到所提供参数的副本中。
由于displayProducts()
不会修改提供的vector
,而是通过const&
:
void displayProducts(const vector<Product>& vect)
有关更简单的范围迭代代码,请参阅 c++11 中引入的range-for。
【讨论】:
非常感谢!这解决了它!以上是关于如何打印出结构向量?它不保存数据吗?的主要内容,如果未能解决你的问题,请参考以下文章