在获取其他函数中的变量后创建数组
Creating array after getting variable in other function
文本文件如下所示:
3
String
String2
String3
我必须创建一个函数来读取文本文件中的所有字符串,将它们保存到数组中,然后在主函数中显示。例如
void reading(int & count, string strings[]) {
ifstream fd "text.txt";
fd >> count;
for (int i=0; i<count; i++)
fd >> strings[i];
fd.close();
主要功能:
int main() {
int count=0;
string strings[100];
reading(count, strings);
for (int i=0; i<count; i++)
cout << strings[i] << endl;
字符串的数目写入文本文件的第一行。如何创建一个恰好包含该数字的数组?我需要它能够在主要/其他功能中访问它。(例如,用于写入另一个文本文件的函数)。
如果有一个非常重要的原因需要对数组执行此操作,那么请使用std::new,如下所示:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int reading(string** str_array, int& size, string filename) {
ifstream infile(filename.c_str());
if(!infile) {
cerr << "Unable to access file!n";
return -1;
}
int n = 0;
infile >> size;
try{
*str_array = new string[size];
string str;
for (; n < size && infile; ++n) {
infile >> str;
(*str_array)[n] = str;
}
if (n != size)
cerr << "ERROR, read less than " << size << " strings!!nn";
} catch(bad_alloc& exc) {
return -2;
}
return 0;
}
int main() {
string* str_array = NULL;
int size;
if(reading(&str_array, size, "test.txt")) {
cerr << "Din't read file, exiting...n";
return -1;
}
for(int i = 0; i < size; ++i)
cout << str_array[i] << endl;
delete [] str_array; // DO NOT FORGET TO FREE YOUR MEMORY
str_array = NULL;
return 0;
}
输出:
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3
然而,您使用的是c++,而不是使用std::vector?
看看它有多简单:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int reading(vector<string>& v, string filename) {
ifstream infile(filename.c_str());
if(!infile) {
cerr << "Unable to access file!n";
return -1;
}
int N = -1, n = 0;
infile >> N;
string str;
for (; n < N && infile; ++n) {
infile >> str;
v.push_back(str);
}
if (n != N)
cerr << "ERROR, read less than " << N << " strings!!nn";
return 0;
}
int main() {
vector<string> v;
if(reading(v, "test.txt")) {
cerr << "Din't read file, exiting...n";
return -1;
}
for(size_t i = 0; i < v.size(); ++i)
cout << v[i] << "n";
return 0;
}
输出:
C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3
编辑:
我们必须传递一个指向我们想要修改的内容(即string*
)的指针,否则我们应用的更改将不会发生。自己测试它,传递一个string*
作为参数,而不是string**
,修改函数的主体,看看会发生什么。
为了得到这个想法,假设我们想写入指针,即new
给我们的新地址,并保存所请求的内存。我们确实在函数中写入了该地址,但当函数终止时,我们希望更改是持久的。请参阅我在C中的函数作为示例。
在堆上分配一个数组,如下所示:
std::string* stringArr = new std::string[(place amout of strings here)];
并且不要忘记在main()的末尾删除
delete stringArr[];
堆上的变量/数组是动态的,因此大小不必固定!
std::string*
→这是一个指针,指向内存中该数组开头的地址。(只是为了让你的电脑知道它在哪里)
stringArr
→阵列的名称
new
→分配新内存
std::string[size]
→说明分配的金额
我看到了一些关于"矢量"的答案。如果你想使用它们,你可以看看这个页面了解更多信息!→矢量文档
使用矢量
#include <vector>
#include<fstream>
#include <iostream>
using namespace std;
void reading(int & count, vector<string> * strings) {
ifstream fd("text.txt");
fd >> count;
string T;
for (int i=0; i<count; i++)
{
fd >> T;
strings->push_back(T);
}
fd.close();
}
int main() {
int count=0;
vector<string> strings;
reading(count, &strings);
for (int i=0; i<count; i++)
cout << strings[i] << endl;
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如果不分配内存,我如何能够为变量创建和分配值?
- 正在头类中为私有变量创建内联函数
- 显示 C++ 中"anonymous"变量创建的警告
- 基于输入变量创建JSON结构
- 使用ROS2环境变量创建C 项目
- 尝试使用自定义类的结构中的变量创建该类的优先级队列
- 为变量创建局部别名会增加开销吗?
- 非静态成员变量创建,如C++中的静态单例创建
- 有没有更好的方法来通过 C/C++ 中的环境变量创建路径
- main () 是使用自动变量创建的,如果是这样,它的目的是什么?
- 我应该用switch语句从变量创建C++模板化对象吗
- 尝试使用值初始化列表,但在使用变量创建大小时不起作用
- 使用变量创建数组
- 为变量创建副本
- <type> 为结构变量创建矢量类型的对象
- 在定义充满成员的结构体时,是否为该结构体类型的每个变量创建这些成员?
- 如何使用要在 cmd 中执行的变量创建命令
- 使用另一个变量创建name变量