我是新手c ++程序员所以如果这是一个天真的问题,请原谅我。我有包含大数组的文件,这些数组包含我之前在javascript应用程序中使用过的数万个字符串。有没有办法将这些包含到C ++源代码中,以便将数组与代码一起编译?
目前,文件被格式化为返回(javascript)文字数组的函数,如下所示:
// javascript array stored in .js text file
function returnMyArray()
{
return ["string1", "string2", "string3", ... "stringBigNumber"];
} // eof returnMyArray()
我用通常的javascript脚本&'包含'外部文件src标记并为数组赋值如下:
myArray = returnMyArray();
我想在c ++中实现等效,即在我的c ++源代码中将存储在文件中的数组分配给数组,以便在编译时可以执行数据。
我认为理论上我可以将文件中的(适当格式化的)数组复制并粘贴到我的c ++源代码中,但它们太大而不实用。
我可以轻松地将文件重新编写为最容易让c ++访问数据的格式 - 以c ++数组语法或每行一个字符串读入数组。
与此类似,在终端中使用g ++进行编译时,是否有一种简单的方法可以包含包含自定义函数库的文件? (我的网络搜索为各种IDE应用程序显示了很多方法,但我在vim中编写源代码并在命令行上使用g ++进行编译)。
我很抱歉,如果这是微不足道的,我已经错过了,但我很难过!
谢谢。
答案 0 :(得分:2)
以下是我的结构:
文件: data.array
/* C++ style comments are ok in this file and will be ignored
* both single and multiline comments will work */
// the data in the array is a comma seperated list, lines can be any length
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
// more comma seperated data
9996, 9997, 9998, 9999
文件: class.h
extern int myArray[]; // you should fill in the size if you can
// more stuff here
文件: class.cpp
// if you have an editor that highlights syntax and errors, it may not like this
// however, #include is handled before compiling and performs a blind substitution
// so this is perfectly legal and should compile.
// Visual C++ 2010 highlights this as an error, but the project builds fine.
int myArray[]
{
#include "data.array"
};
// other definitions of stuff in class.h