C++在一个命名空间中填充一个向量。

huangapple go评论54阅读模式
英文:

C++ Fill a vector in a namespace

问题

以下是对Python代码的C++翻译:

foo.h

#ifndef FOO_H
#define FOO_H

#include <vector>
#include <string>
#include "consts.h"

namespace bar {
    extern std::vector<std::string> table;
    std::string UseTable(int index);
} // namespace bar

#endif // FOO_H

foo.cc

#include "foo.h"
#include "consts.h"

namespace bar {
    std::vector<std::string> table(consts::size_of_table, "");

    std::string UseTable(int index) {
        return table[index];
    }
} // namespace bar

这个C++代码保留了Python代码的功能,包括初始化表并提供使用表的功能。 C++中的table是在foo.cc中定义的全局std::vector,并在foo.h中声明为extern,以便在其他源文件中使用。 UseTable函数提供了与Python代码中的use_table相似的功能。 你不需要构造函数或初始化钩子,因为全局table在程序启动时初始化。

如果需要在不同源文件中使用table,确保在这些文件中包括foo.h并链接foo.cc

英文:

I am trying to port the following Python code to C++:

foo.py

import settings.consts as consts

table = [&quot;&quot;] * consts.table_size
for idx in range(0, consts.table_size):
    str = consts.lookup[get_by_index(idx)]
    table[idx] = str

def use_table(index):
    return table[index]

It initializes one time a table with the size taken from the settings. Then the table is filled up with values in a for loop. use_table is a simplified function which consumes the table.

Here is my attempt in C++:

foo.h

#ifndef FOO_H
#define FOO_H

#include &lt;vector&gt;
#include &lt;string&gt;
#include &quot;consts.h&quot;

namespace bar {

    std::vector&lt;std::string&gt; table(consts::size_of_table, &quot;&quot;);
    int GetByIndex(int a);
    string UseTableable(int index);

} // namespace bar

#endif // FOO_H

foo.cc

string UseTableable(int index)
{
  return table[index];
}

The problem

As far as I know, in C++ the for loop must be in a function. I could make a function to fill up the vector, but when should I call it? Namespaces do not have a constructor, or initialization hooks.

What would be the equivalent architect to the original Python code?

答案1

得分: 3

你在想要初始化向量时调用该函数。

你可以使用 lambda 函数,

std::vector<std::string> table = 
    [](){
        std::vector<std::string> tbl(consts::size_of_table, "");
        for (size_t i = 0; i < tbl.size(); i++) {
            tbl[i] = consts::lookup[GetByIndex(i)];
        }
        return tbl;
    }();

或者一个命名函数,

std::vector<std::string> make_table()
{
    std::vector<std::string> tbl(consts::size_of_table, "");
    for (size_t i = 0; i < tbl.size(); i++) {
        tbl[i] = consts::lookup[GetByIndex(i)];
    }
    return tbl;
}

std::vector<std::string> table = make_table();
英文:

You call the function when you want to initialize the vector.

You can use a lambda function,

std::vector&lt;std::string&gt; table = 
    [](){
            std::vector&lt;std::string&gt; tbl(consts::size_of_table, &quot;&quot;);
            for (size_t i = 0; i &lt; tbl.size(); i++) {
                tbl[i] = consts::lookup[GetByIndex(i)];
            }
            return tbl;
    }();

or a named function,

std::vector&lt;std::string&gt; make_table()
{
    std::vector&lt;std::string&gt; tbl(consts::size_of_table, &quot;&quot;);
    for (size_t i = 0; i &lt; tbl.size(); i++) {
        tbl[i] = consts::lookup[GetByIndex(i)];
    }
    return tbl;
}

std::vector&lt;std::string&gt; table = make_table();

答案2

得分: 0

你可以在程序的 main() 函数中进行初始化,例如:

foo.h

#ifndef FOO_H
#define FOO_H

#include <vector>
#include <string>

namespace bar {

    extern std::vector<std::string> table;

    void InitTable();
    int GetByIndex(int a);
    std::string UseTable(int index);

} // namespace bar

#endif // FOO_H

foo.cpp

#include "foo.h"
#include "consts.h"

std::vector<std::string> bar::table;

void bar::InitTable()
{
    table.resize(consts::size_of_table, "");
    for(int idx = 0; idx < consts::size_of_table; ++idx) {
        table[idx] = consts::lookup[GetByIndex(idx)];
    }

    /* 或者:
    table.reserve(consts::size_of_table);
    for(int idx = 0; idx < consts::size_of_table; ++idx) {
        table.push_back(consts::lookup[GetByIndex(idx)]);
    }
    */
}

int bar::GetByIndex(int a)
{
    return ...;
}

std::string bar::UseTable(int index)
{
  return table[index];
}

main.cpp

#include "foo.h"

int main()
{
    bar::InitTable();
    // 根据需要使用表格...
    return 0;
}

希望这对你有所帮助。

英文:

You can do the initialization in your program's main() function, eg:

foo.h

#ifndef FOO_H
#define FOO_H

#include &lt;vector&gt;
#include &lt;string&gt;

namespace bar {

    extern std::vector&lt;std::string&gt; table;

    void InitTable();
    int GetByIndex(int a);
    std::string UseTable(int index);

} // namespace bar

#endif // FOO_H

foo.cpp

#include &quot;foo.h&quot;
#include &quot;consts.h&quot;

std::vector&lt;std::string&gt; bar::table;

void bar::InitTable()
{
    table.resize(consts::size_of_table, &quot;&quot;);
    for(int idx = 0; idx &lt; consts::size_of_table; ++idx) {
        table[idx] = consts::lookup[GetByIndex(idx)];
    }

    /* alternatively:
    table.reserve(consts::size_of_table);
    for(int idx = 0; idx &lt; consts::size_of_table; ++idx) {
        table.push_back(consts::lookup[GetByIndex(idx)]);
    }
    */
}

int bar::GetByIndex(int a)
{
    return ...;
}

string bar::UseTable(int index)
{
  return table[index];
}

main.cpp

#include &quot;foo.h&quot;

int main()
{
    bar::InitTable();
    // use table as needed...
    return 0;
}

huangapple
  • 本文由 发表于 2023年3月21日 00:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定