C++编程从外部文本文件定义数组大小
C++ programming define array size from the external text file
我试图使我的数组具有非常量值的大小。大小应由从中获取信息的"test.txt"文件定义。例如,如果 txt 文件有 10 个数字,则数组的大小应为 10。我尝试使用矢量,但我无法使其工作。任何帮助将不胜感激。下面是代码:
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<cstdlib>
using namespace std;
/* Function to print an array
A int[]: an array of n element
n int; length of an array
*/
void displayList(float A[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << A[i] << ", ";
cout << endl;
}
/*
Insertion Sort function
A int[]: an array of n element
n int; length of an array
*/
void insertionSort(float A[], int n)
{
int i, j;
float key;
for (i = 1; i < n; i++)
{
key = A[i];// take key
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && A[j] > key)
{
A[j + 1] = A[j]; // move element to next postion
j = j - 1; // reduce index of j - go backward in the array
}
std::cout << "Step key at i = " << i << ": [" << key << "] inserted at j = " << j + 1 << "
position -> ";
A[j + 1] = key; // at j+1 th position we place the key
displayList(A, n);
}
};
ifstream input("test.txt"); //put your program together with thsi file in the same folder.
int main() {
int const ARRAY_SIZE = 9;
float A[ARRAY_SIZE];
string line;
ifstream inFile;
int i = 0, cnt = 0;
float n;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (!inFile.eof()) {
getline(inFile, line);
n = atof(line.c_str());
cnt++;
}
int cnt;
cin >> cnt;
vector<float> A(cnt);
inFile.close();
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (!inFile.eof()) {
getline(inFile, line);
n = atof(line.c_str());
A[cnt++] = n;
}
inFile.close();
n = sizeof(A) / sizeof(A[0]);
cout << "insertionSort: n";
cout << "Unsorted array: ";
displayList(A, n);
insertionSort(A, n);
std::cout << "Sorted array: ";
displayList(A, n);
}
来自 TXT 文件的示例输入:
12
四
5
9
6
11
0
阿拉伯数字
0,5
要使其与vector
一起使用,您不应该创建具有许多元素的向量,例如vector<float> v(10);
。创建一个空vector
,并一次向其添加一个值。
void display(const std::vector<float>& A) {
std::cout << "Got " << A.size() << " elements.n";
for(float value : A) {
std::cout << value << 'n';
}
}
int main() {
std::vector<float> A; // an empty vector of floats
float temp; // a temporary float to use for extraction
while(input >> temp) { // loop while extraction succeeds
A.push_back(temp); // save the value at the end of the vector
}
display(A);
}
相关文章:
- C++编程从外部文本文件定义数组大小
- 如何使用数组 (C++) 读取具有未定义数量的值的 .txt 文件?
- 数组大小由输入文件中的行数定义
- 在另一个文件中初始化的 extern 数组中未定义的引用
- 为什么我的 2D 数组未定义?
- 通过自定义分隔符从文件中读取对象数组
- shared_ptr<>到数组自定义删除器(带make_shared)
- const 数组,用于在数组长度定义中使用其元素或为模板参数提供值
- 在不同的文件(C++)中定义指针数组长度
- 如何使用类 .h 文件中的静态常量来定义数组的长度
- 如何将自定义数据类型(用std::对填充的std::数组)写入文件流
- C++数组类定义和实现,未在作用域中声明
- 在头文件的数组中定义资源 ID
- 如何处理C++中在数组上定义的函数
- c++和Java数组声明/定义:差异
- 读取二维数组文件
- C++ 我知道数组长度,但想知道是否是一种更简洁的数组元素定义方法
- 使用C++数组输入定义重载函数是安全的
- 堆积或堆叠数组.用户定义的类型难题
- 通过无符号整数读取无符号字符数组未定义,因此不安全