首页 \ 问答 \ 在C ++中初始化vector >>(Initialize a vector > > in C++)

在C ++中初始化vector >>(Initialize a vector > > in C++)

我有双向量传染媒介传染媒介。 我正在尝试初始化它。 以下代码段错误。 包括注释代码也没有帮助(不编译)。

vector<vector<vector<double> > > Q(MAX_GRID);
for(int row = 0; row < MAX_GRID; row++) {
    //vector<vector<double> > inQ(MAX_GRID);
    //Q[row].push_back(inQ);
    for(int col = 0; col < MAX_GRID; col++)
        for(int action = 0; action <= 3; action++)
            Q[row][col].push_back(0);
}

I have a vector of vector of vector of doubles. I'm trying to initialize it. The below code seg faults. Including the commented code also does not help (does not compile).

vector<vector<vector<double> > > Q(MAX_GRID);
for(int row = 0; row < MAX_GRID; row++) {
    //vector<vector<double> > inQ(MAX_GRID);
    //Q[row].push_back(inQ);
    for(int col = 0; col < MAX_GRID; col++)
        for(int action = 0; action <= 3; action++)
            Q[row][col].push_back(0);
}

原文:https://stackoverflow.com/questions/15872795
更新时间:2023-04-14 13:04

最满意答案

Q1:如何链接来自不同子目录的目标文件?

假设你的程序prog是一个C程序,它将从对象file0.ofile1.o链接,这些对象将被编译到子目录obj 。 以下是您在makefile中通常需要执行的链接。

$(OBJS) = $(patsubst %.o,obj/%.o,file0.o file1.o)

prog: $(OBJS)
    gcc -o $@ $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS)

这使得$(OBJS) = obj/file0.o, obj/file1.o并且只需将这样的对象文件传递给link命令。 patsubst文档

注意如果你想编译一个目标文件到目标文件中,这个目录不存在就不足以创建 obj子目录。 你必须自己创造它或研究如何make做到这一点。

Q2:如何包含不同的搜索路径?

这是一个含糊不清的问题 - 模棱两可令你感到困惑 - 我们必须将其分解为Q2.a,Q2.b,Q2.c

Q2.a:如何指定不同的搜索路径,预处理器将查找源代码中#include -ed的头文件?

默认情况下,预处理器将使用内置的标准搜索路径列表查找头文件。 您可以通过在详细模式下运行预处理器来查看它们,例如cpp -v (CTRL-C终止)。 输出将包含如下内容:

#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

假设您在子目录incinc/foo/bar有一些您自己的头文件,并且希望预处理程序也搜索这些目录。 然后您需要传递预处理器选项:

-I inc -I inc/foo/bar

到你的编译命令。 预处理器选项通常分配给make变量CPPFLAGS ,例如

CPPFLAGS = -I inc -I inc/foo/bar

(以及您需要的任何其他预处理器选项),并在编译命令配方中通过此变量传递,例如

gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<

注意:认为CPPFLAGS是C ++编译器标志的常规make变量是一个常见的错误。 C ++编译器标志的常规make变量是CXXFLAGS

运行以下命令可以看到-I选项的效果:

mkdir -p inc/foo/bar # Just to create the path
cpp -v -I inc -I inc/foo/bar

(CTRL-C终止)。 现在输出将包含类似于:

#include "..." search starts here:
#include <...> search starts here:
 inc
 inc/foo/bar
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Q2.b:如何指定链接器将查找库的不同搜索路径?

假设您有一个库, libfoobar.a ,您需要与prog链接,并且它驻留在一个目录lib ,该lib位于makefile的上两级。 然后你需要传递链接器选项:

-L ../../lib

-lfoobar

到你的链接命令。 其中的第一个将告诉链接器../../lib是查找库的地方之一。 通常,您通过LDFLAGS在链接器命令配方中传递此选项。 第二个告诉连接器搜索一个名为libfoobar.a (一个静态库)或libfoobar.so (一个动态库)的库。 通常,您通过LDLIBS在链接器命令配方中传递此选项

就像预处理程序的默认搜索路径列表一样,链接程序也有默认的搜索路径列表。 您可以通过运行来查看它们:

gcc -v -Wl,--verbose 2>&1 | grep 'LIBRARY_PATH'

输出结果如下所示:

LIBRARY_PATH = / usr / lib / gcc / x86_64-linux-gnu / 4.8 /:/ usr / lib / gcc / x86_64-linux-gnu / 4.8 /../../../ x86_64-linux-gnu / usr / lib / gcc / x86_64-linux-gnu / 4.8 /../../../../ lib /:/ lib / x86_64-linux-gnu /:/ lib /../ lib /:/ usr /lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/: / usr / lib中/

当您需要链接其中一个默认库搜索路径中的标准库之一(例如libm (数学库))时,您不需要传递任何-L选项。 -lm独自会做。

Q2.c:如何指定不同的搜索路径, make将查找目标的先决条件?

注意这是一个关于make的问题,而不是关于预处理器,编译器或链接器的问题。

我们假定所有的目标文件都将被编译到子目录obj 。 为了让它们在那里编译,只需使用模式规则就会很好,很简单:

obj/%.o:%.c
    gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<

这告诉make ,例如obj/file0.o是由file0.c通过配方创建的:

gcc -c -o obj/file0.o $(CPPFLAGS) $(CFLAGS) file0.c

对于任何文件obj/*.o和匹配文件*.c相似的

这很好,只要file0.c驻留在与makefile相同的目录中,但是假设你的*.c文件在其他地方? 假设你的源文件被组织在子目录foo/file0.cbar/file1.c 。 然后make将无法满足该模式规则,并会说“没有规则来制作目标obj / file0.o”等。

为了解决这个问题,使用VPATH ,一个具有特殊含义的make变量。 如果您将目录名称列表(以':'为VPATH分配给VPATH ,那么只要在当前目录中找不到目录名称, make就会在每个列出的目录中搜索先决条件。 所以:

VPATH = foo:bar

会导致make在当前目录中查找,然后在尝试查找.c文件以符合该模式规则时查找foobar 。 它将成功满足规则并编译必要的源文件。

注意您在发布的代码中错误地使用了VPATH

VPATH = -L/home/dkumar/libtsnnls-2.3.3/tsnnls

您已经为其链接器搜索路径分配了链接器选项-L ,该链接器选项在那里没有业务。

底线:

  • 预处理程序的搜索路径(用于定位头文件)由预处理程序的-I<dirname>选项指定。 将这些选项传递给CPPFLAGS的编译配方

  • 用于定位库的链接程序搜索路径通过链接程序的-L<dirname>选项指定。 将这些选项传递给LDFLAGS的链接配方

  • make规则的preprequisities的搜索路径在make变量VPATH为一个':-punctuated目录名称列表。


Q1: How to link object files from a different subdirectory?

Let's say your program prog is a C program that will be linked from object file0.o, file1.o which are to be compiled into subdirectory obj. Here is the sort of thing you typically need in your makefile to perform that linkage.

$(OBJS) = $(patsubst %.o,obj/%.o,file0.o file1.o)

prog: $(OBJS)
    gcc -o $@ $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS)

This makes $(OBJS) = obj/file0.o, obj/file1.o and you simply pass the object files like that to the link command. Documentation of patsubst

N.B. This is not sufficient to create the obj subdirectory if it doesn't exist when you want to compile an object file into it. You'll have to create it yourself or study how to make make do it.

Q2: How to include different search paths?

This is an ambiguous question - the ambiguity is confusing you - and we must break it down into Q2.a, Q2.b, Q2.c:

Q2.a: How to specify different search paths where the preprocessor will look for header files that are #include-ed in the source code?

By default, the preprocessor will look for header files using a built-in a list of standard search paths. You can see them by running the preprocessor in verbose mode, e.g.cpp -v (CTRL-C to terminate). The output will contain something like:

#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Let's suppose you have some header files of your own in subdirectories inc and inc/foo/bar and want the preprocessor to search these directories as well. Then you need to pass the preprocessor options:

-I inc -I inc/foo/bar

to your compile command. Preprocessor options are conventionally assigned to the make variable CPPFLAGS, e.g.

CPPFLAGS = -I inc -I inc/foo/bar

(along with any other preprocessor options you require), and passed via this variable in the compile command recipe, e.g.

gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<

N.B. It is a common mistake to think that CPPFLAGS is the conventional make variable for C++ compiler flags. The conventional make variable for C++ compiler flags is CXXFLAGS.

You can see the effect of the -I option by running:

mkdir -p inc/foo/bar # Just to create the path
cpp -v -I inc -I inc/foo/bar

(CTRL-C to terminate). Now the output will contain the like of:

#include "..." search starts here:
#include <...> search starts here:
 inc
 inc/foo/bar
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Q2.b: How to specify different search paths where the the linker will look for libraries?

Let's suppose you are have a library, libfoobar.a that you need to link with prog and that it resides in a directory lib that is 2 levels up from your makefile. Then you need to pass the linker options:

-L ../../lib

and

-lfoobar

to your link command. The first of these will tell the linker that ../../lib is one of the places to look for libraries. Conventionally, you pass this option in the linker command recipe via LDFLAGS. The second tells the linker to search for some library called libfoobar.a (a static library) or libfoobar.so (a dynamic library). Conventionally, you pass this option in the linker command recipe via LDLIBS

Just as there is a default list of search paths for the preprocessor, there is a default list of search paths for the linker. You can see them by running:

gcc -v -Wl,--verbose 2>&1 | grep 'LIBRARY_PATH'

The output will be something like:

LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/: /usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/

When you need to link one of the standard libraries, e.g. libm (the math library), that resides in one of the the default library search paths, you don't need to pass any -L option. -lm alone will do.

Q2.c: How to specify different search paths where make will look for the prerequisites of targets?

N.B. This is a question about make, not a question about the preprocessor, compiler, or linker.

We have assumed that all of your object files will be compiled into the subdirectory obj. To get them compiled there, it would be nice and simple just to use the pattern rule:

obj/%.o:%.c
    gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<

which tell make that, e.g. obj/file0.o is made from file0.c by the recipe:

gcc -c -o obj/file0.o $(CPPFLAGS) $(CFLAGS) file0.c

and similarly for any file obj/*.o and matching file *.c

This is fine as long file0.c resides in the same directory as the makefile, but suppose you have your *.c files somewhere else? Say your source files are organised in subdirectories, foo/file0.c and bar/file1.c. Then make will be unable to satisfy that pattern rule and will say there is "no rule to make target obj/file0.o", etc.

To solve this problem use VPATH, a make variable that has a special meaning. If you assign a list of directory names, punctuated by ':', to VPATH, then make will search for a prerequisite in each of the listed directories whenever it can't find it in the current directory. So:

VPATH = foo:bar

will cause make to look in the current directory, and then foo and bar when it tries to find .c files to match that pattern rule. It will succeed in satisfying the rule and will compile the necessary source files.

N.B. You have used VPATH wrongly in your posted code:

VPATH = -L/home/dkumar/libtsnnls-2.3.3/tsnnls

You have asigned a linker search path to it, with the linker option -L, which has no business there.

Bottom line:

  • The preprecessor search paths, for locating header files, are specified with the preprocessor's -I<dirname> option. Pass these options to the compile recipe in CPPFLAGS

  • The linker search paths, for locating libraries, are specified with the linker's -L<dirname> option. Pass these options to the linkage recipe in LDFLAGS

  • The search paths for the preprequisities of make rules are specified in the make variable VPATH, as a ':-punctuated list of directory names.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。