如何将元素从向量转移到新数组?
How to transfer elements from vector to a new array?
我需要将元素从总成本(int totalCost(转移到一个新的数组中,称为cost[]。我尝试在代码末尾执行此操作,但是当我尝试访问数组中的第一个元素时,它显示了输出中的所有总成本。我只需要访问第一个。
// ******** CREATE "example.txt" IN THE FOLDER THAT HAS THE PROJECT'S .CPP FILE ********
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <iterator>
using namespace std;
class vehicle_capacities {
public:
int lw, vw;
};
double nearest_ten(double n)
{
return round(n / 10.0 + 0.4) * 10.0;
}
bool cmp_fn(int a, int b)
{
return a > b;
}
int main()
{
int cw;
vehicle_capacities cap;
cap.lw = 30;
cap.vw = 10;
ifstream myfile("example.txt");
if (myfile.is_open()) {
myfile >> cw;
myfile.close();
}
else {
cout << "Unable to open file" << endl;
}
cout << "Amount of cargo to be transported: " << cw;
cw = nearest_ten(cw);
cout << "(" << cw << ")" << endl;
int maxl = cw / cap.lw; // maximum no. of lorries that can be there
vector<pair<int, int>> solutions;
//vector<int> costs;
vector<int>::iterator it;
// for the inclusive range of 0 to maxl, find the corresponding no. of vans for each variant of no of lorries
for (int l = 0; l <= maxl; ++l) {
bool is_integer = (cw - l * cap.lw) % cap.vw == 0; // only if this is true, then there is an integer which satisfies for given l
if (is_integer) {
int v = (cw - l * cap.lw) / cap.vw; // no of vans
solutions.push_back(make_pair(l, v));
}
}
cout << "Number of mini-lorries: ";
for (auto& solution : solutions) {
cout << solution.first << " ";
}
cout << "n";
cout << "Number of vans: ";
for (auto& solution : solutions) {
cout << solution.second << " ";
}
cout << "n";
cout << "Total cost: ";
// LORRY COST = $200, VAN COST = $45
for (auto& solution : solutions) {
int totalCosts = (solution.first * 200) + (solution.second * 45);
cout << totalCosts << " ";
}
/*for (auto& solution : solutions) {
int totalcosts = (solution.first * 200) + (solution.second * 45);
costs.push_back(totalcosts);
for (it = costs.begin(); it < costs.end(); it++) {
cout << *it << " ";
}
}*/
cout << endl;
// Comparison between both vehicles, highest amount = trips needed
cout << "Trips Needed: ";
for (auto& solution : solutions) {
int a = solution.first;
int b = solution.second;
if (a > b) {
cout << a << " ";
}
else if (b > a) {
cout << b << " ";
}
else if (a == b) {
cout << a << " ";
}
}
cout << endl;
cout << "Lowest #1: ";
for (auto& solution : solutions) {
int totalCosts[] = { (solution.first * 200) + (solution.second * 45) };
int elements = sizeof(totalCosts) / sizeof(totalCosts[0]);
sort(totalCosts, totalCosts + elements, cmp_fn);
for (int i = 0; i < elements; ++i) // print the results
cout << totalCosts[i] << " ";
cout << totalCosts[0] << " ";
}
// *** FOR SORTING ELEMENTS IN ARRAY LOW TO HIGH ***
/*int array[] = { 1,10,21,55,1000,556 };
int elements = sizeof(array) / sizeof(array[0]); // Get number of elements in array
sort(array, array + elements);
for (int i = 0; i < elements; ++i) // print the results
cout << array[i] << ' ';*/
return 0;
}
如果您有任何解决方案,请更新,谢谢。
(请注意,您必须在项目文件中创建"example.txt"。
您需要动态分配数组。
int elements = solution.size();
int *totalCosts = new int[elements];
int j= 0;
// transfer data
for (auto& solution : solutions) {
totalCosts[j++] = (solution.first * 200) + (solution.second * 45);
}
sort(totalCosts, totalCosts + elements, cmp_fn);
for (int i = 0; i < elements; ++i) // print the results
cout << totalCosts[i] << " ";
cout << totalCosts[0] << " ";
delete[] totalCosts;
相关文章:
- 新的整数数组向右移动?
- 标量"新 T "与数组"新 T[1]"
- 如何将标准::字符串内容克隆到新的 char[] 数组
- C++ strcpy_s复制到新的字符数组时引发错误
- 无限数组C++在一个表达式中使用两个新值调整数组大小
- 新动态字符数组末尾的空字符
- c ++新的C数组分配,RAII或简单的shared_ptr/boost::shared_array
- 返回新分配的数组
- 数组的末尾指向新分配的数组C++
- 如何创建新的字符串数组
- 动态数组:将中两个动态数组中元素的值添加到新的动态数组中
- 一组数组添加到一个新数组
- 将新值写入数组
- 按值传递字符数组,并从函数返回一个新的字符数组
- 用不同的指定值初始化新的c++数组
- [C++]在此代码之前不存在的析构函数中引发的内存错误。尝试创建一个新的动态数组并填充它
- 动态分配一个新的整数数组
- 将两个字符串的相似性写入新的动态数组的最简单方法
- 如何在C++中动态创建新的 2D 数组
- 分配新的布尔数组指针