📄 arrays.cpp
字号:
/*
Sample Array Declaration and Use
File: ARRAYS.CPP
Version: 3.0
Created: 9/26/96
Last Updated: 10/20/99
Written by: Dr. C. S. Tritt
*/
#include <iostream> // Header for stream I/O
#include <fstream> // Header for file I/O
#include <iomanip> // Header for I/O stream manipulators
#include <vector> // Header for vector class
using namespace std;
// Declare function(s).
double asum(vector<double> const& vector_to_sum, int number_to_sum);
int main(void)
{
// Declare and initialize variables.
const int MAX_SIZE = 10; // The const modifier assures that size can't be changed.
int size = 0; // Actual size of the array (counting from zero).
int count; // Loop counter (will be initialized in for loop).
vector<double> a(MAX_SIZE); // Declare the array to hold MAX_SIZE values.
ifstream fin; // Declare an input file stream.
// Open the input file and check for errors.
fin.open("asum.in", ios::in);
if (!fin)
{
cout << "Can't open input file\n";
return 1;
}
// Read array from a file and check for errors.
while ((size < MAX_SIZE) && (fin >> a[size])) ++size; // Short circuit evaluation required.
if (fin.fail() && !fin.eof())
{
cout << "Error while reading line " << size + 1 << ". Aborting.\n";
fin.close();
return 1;
}
else if (size == MAX_SIZE) cout << "Array full. Some data may be ignored.\n";
// Echo it.
cout << showpoint << setprecision(3); // Set reasonable output format.
cout << "After read A is: ";
for (count = 0; count < size; ++count) cout << a[count] << " ";
cout << endl;
// Call the asum function.
cout << "The sum of its elements is: " << asum(a, size) << "\n";
// Create a second vector, initialize it with ones and display it.
vector<double> spare(MAX_SIZE, 1.0);
cout << "After creation spare is: ";
for (count = 0; count < MAX_SIZE; ++count) cout << spare[count] << " ";
cout << endl;
// Show sizes, resize and show sizes again.
cout << "Size of vector a: " << a.size() << " and ";
cout << "Size of vector spare: " << spare.size() << endl;
a.resize(size);
spare.resize(size);
cout << "After resize to size:\n";
cout << "Size of vector a: " << a.size() << " and ";
cout << "Size of vector spare: " << spare.size() << endl;
// Remove element size/2 from a, insert it into spare and display results.
int element = size / 2;
spare.insert(spare.begin() + element, a[element]); // .begin() required to produce iterator type.
a.erase(a.begin() + element); // .begin() required to produce iterator type.
cout << "A: ";
for (count = 0; count < size - 1; ++count) cout << a[count] << " ";
cout << endl;
cout << "Spare: ";
for (count = 0; count < size + 1; ++count) cout << spare[count] << " ";
cout << endl;
// Swap a and spare.
a.swap(spare);
cout << "After swap:\n";
cout << "A: ";
for (count = 0; count < size + 1; ++count) cout << a[count] << " ";
cout << endl;
cout << "Spare: ";
for (count = 0; count < size - 1; ++count) cout << spare[count] << " ";
cout << endl;
// Test empty on spare, clear it and try again.
cout << "Before clear... ";
cout << "spare.empty() = " << spare.empty() << " and ";
cout << "size of spare = " << spare.size() << endl;
spare.clear();
cout << "After clear... ";
cout << "spare.empty = " << spare.empty() <<" and ";
cout << "size of spare = " << spare.size() << endl;
// Close file and return a zero if everything worked.
fin.close();
return 0;
}
// Define the function.
double asum(vector<double> const& a, int n)
{
// This function returns the sum of the first n (or all) values in array a.
if (n >= a.size())
{
cout << "Requested size greater than array size, summing all elements.\n";
n = a.size();
}
double sum = 0.0; // This is the running sum.
int count; // This is the loop counter.
for (count = 0; count < n; ++count) sum = sum + a[count];
return sum;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -