在c++中向数组结构中添加元素
Adding elements to array struct in c++
我在c++上做一个赋值,我卡住了如何在我的工作中添加一个新的事务,使用用户定义的numShares和pricePerShare。
我有一个像这样的事务结构:
struct Transaction
{
string stockSymbol; // String containing the stock symbol, e.g. "AAPL"
string buyerName; // String containing the buyer's name e.g. "Mr Brown"
int buyerAccount; // Integer containing an eight digit account code
int numShares; // Integer containing the number of sold shares
int pricePerShare; // Integer containing the buy price per share
};
这是buildTransaction类:
static Transaction* buildTransactions(int numTransactions)
{
int maxShareVolume = 100000;
int maxSharePrice = 1000;
Transaction *transactions = new Transaction[numTransactions];
for(int idx = 0; idx < numTransactions; idx++)
{
transactions[idx].stockSymbol = pickRandomStockSymbol();
std::string buyerName = pickRandomBuyer();
transactions[idx].buyerName = buyerName;
transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);
transactions[idx].numShares = 1 + rand() % maxShareVolume;
transactions[idx].pricePerShare = 1 + rand() % maxSharePrice;
}
return transactions;
}
我如何使用它来添加数据到事务数组中呢?
void Analyser::addTransactions(Transaction* transactions, int numTransactions)
我将从这里假设所有我真正需要的用户输入将是股票数量和每股价格,但其他信息将自动填充,从数组中选择。
不应该使用数组,而应该使用向量。buildTransactions应该这样写:
std::vector<Transaction> buildTransactions(int numTransactions)
{
int maxShareVolume = 100000;
int maxSharePrice = 1000;
std::vector<Transaction> transactions;
for(int idx = 0; idx < numTransactions; idx++)
{
Transaction t;
t.stockSymbol = pickRandomStockSymbol();
std::string buyerName = pickRandomBuyer();
t.buyerName = buyerName;
t.buyerAccount = lookupBuyerAccount(buyerName);
t.numShares = 1 + rand() % maxShareVolume;
t.pricePerShare = 1 + rand() % maxSharePrice;
transactions.push_back(t);
}
return transactions;
}
通过编辑buildTransactions函数,您可以通过对addTransactions函数执行以下操作轻松地添加更多数据:
void Analyser::addTransactions(std::vector<Transaction> &transactions, int numTransactions)
{
for(int idx = 0; idx < numTransactions; idx++)
{
Transaction t;
t.stockSymbol = pickRandomStockSymbol();
std::string buyerName = pickRandomBuyer();
t.buyerName = buyerName;
t.buyerAccount = lookupBuyerAccount(buyerName);
std::cout << "Enter number of shares for transaction: ";
std::cin >> t.numShares;
std::cout << "Enter price per share for transaction: ";
std::cin >> t.pricePerShare;
transactions.push_back(t);
}
}
希望有帮助
您可以在addTransactions()方法的循环中获得股票数量和每股价格作为输入,如下所示:
for(int idx = 0; idx < numTransactions; idx++)
{
transactions[idx].stockSymbol = pickRandomStockSymbol();
std::string buyerName = pickRandomBuyer();
transactions[idx].buyerName = buyerName;
transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);
ctd::cout<<"Enter number of shares for transaction "<<idx+1<<std::endl;
std::cin>>transactions[idx].numShares;
ctd::cout<<"Enter price per share for transaction "<<idx+1<<std::endl;
std::cin>>transactions[idx].pricePerShare;
}
相关文章:
- 有没有办法向这个向量添加元素?
- 如何在提升间进程中构建具有给定计数的向量并向其添加元素
- 如何在 Arduino 字符串的开头添加元素.类似于 JS unshift();
- C++向数组添加元素并调整数组大小
- 我无法向 c++ 添加元素
- BST 不添加元素
- 调整数组大小并添加元素 C++
- 向向量添加元素
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- 双链接列表添加元素不起作用,不知何故它总是保持为空
- 当我向 vector<int *> 添加元素时,我遇到了一些问题
- 动态地添加元素
- 静态2D数组,添加元素
- 在一个类中向向量添加元素不适用于其他类
- 在数组中添加元素将不接受类似的元素
- STD :: MAP-使用下标操作员与插入方法添加元素
- 动态数组添加元素而不创建结构变量/对象
- 以下向 c++ 向量添加元素的方法有什么区别
- 如何在 std::map 中添加元素自己进行分配
- C++在向量中添加元素的双精度