在循环内将字符串和运行索引连接到字符串中

发布于 2024-08-31 04:01:28 字数 763 浏览 3 评论 0原文

要使用给定的图形包,我需要定义、预订和填充直方图。 如何获取直方图的名称,它是一个要连接的字符串 用 2 个整数作为字符串 ( hts_i_j ) 在 3 个 for 循环中代替。 这必须在 C++ 中完成 请参阅下面的示例

来定义

TH1F* hts_5_53;
TH1F* hts_5_54;
……
TH1F* hts_5_69;

hts_5_53= HDir.make<TH1F>("hts_5_53")," Title", 100,0.,100.);
hts_5_54->HDir.make<TH1F>("hts_5_54")," Title", 100,0.,100.);
……
hts_16_69->HDir.make<TH1F>("hts_16_69")," Title", 100,0.,100.);

预订

hts_5_53->Fill(f)
hts_5_54->Fill(f)
……
hts_16_69->Fill(f)

并填写 3 个 for 循环。 例如 。

for(int i=5, i<17, ++i){
  for(int j=53, j<70, ++j){

   hts_i_j 

 } 
}

我怎样才能让字符串 hts 与 在定义时以简单的简短方式索引(i,j), 预订并填写 3 个 for 循环

To use a given graphic package I need to define, book and fill histogram.
How can I get the name of the histogram which is a string to concatenate
with 2 integer as a string ( hts_i_j ) in 3 for loop instead.
That has to be done in c++
See the exemple below

to define

TH1F* hts_5_53;
TH1F* hts_5_54;
……
TH1F* hts_5_69;

to book

hts_5_53= HDir.make<TH1F>("hts_5_53")," Title", 100,0.,100.);
hts_5_54->HDir.make<TH1F>("hts_5_54")," Title", 100,0.,100.);
……
hts_16_69->HDir.make<TH1F>("hts_16_69")," Title", 100,0.,100.);

to fill

hts_5_53->Fill(f)
hts_5_54->Fill(f)
……
hts_16_69->Fill(f)

Instead I would like to define, book and fill in 3 for loops.
e.g
.

for(int i=5, i<17, ++i){
  for(int j=53, j<70, ++j){

   hts_i_j 

 } 
}

how can I get the string hts to concatenate with the
indices ( i,j) in a simple short way while defining,
booking and filling in 3 for loop instead

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-09-07 04:01:29

我不太明白你的问题。

为“书”声明变量后,您正在对它们做什么?如果将它们放入某种集合中,则可能不需要为每个集合声明一个变量。只需在内部 for 循环中声明一个通用变量,根据需要分配它,然后将其放入您需要的任何集合中。

或者,如果您有一个程序只需要处理几本书,那么就按照您现在的方式进行操作,无需循环。只需手动声明每一项即可。

I don't really understand your question.

What are you doing with the "books" after you declare variables for them? If you are putting them in some sort of collection it may not be necessary to have a variable declared for each one. Just declare a generic variable inside the inner for loop, assign it as needed, and put it in whatever collection you need.

Or if you have a program that just needs to work with a handful of books, then do it exactly how you're doing it now, without loops. Just declare each one manually.

-黛色若梦 2024-09-07 04:01:29

标准 C++ 字符串流是字符串格式化的基本解决方案:

std::ostringstream os;
os << "hts_" << i << "_" << j;

使用 os.str() 检索结果字符串。有关替代方案的讨论,请参阅Manor farm 的字符串格式化程序”< /em>。

有了结果字符串和合适的容器,你的循环可能会变成:

for (int i=5; i<17; ++i) {
    for (int j=53; j<70; ++j) {
       hts[i][j] = f(str); 
    } 
}

为什么你'我不清楚是否需要首先将索引存储在字符串中。

旁注:如果您正在学习一本不涉及字符串格式化和容器合理使用的书,请选择一本好的入门书来自 SO 图书指南

In standard C++ string streams are the basic solution for string formatting:

std::ostringstream os;
os << "hts_" << i << "_" << j;

Retrieve the resulting string with os.str(). For a discussion of alternatives see "The string formatters of Manor farm".

With the resulting string and a suitable container, your loop could e.g. become:

for (int i=5; i<17; ++i) {
    for (int j=53; j<70; ++j) {
       hts[i][j] = f(str); 
    } 
}

Why however you'd need to store the indices in the string in the first place is not clear to me.

Side-note: If you are learning from a book that doesn't cover string formatting and sensible use of containers, choose a good introductory one from SOs book guide.

说不完的你爱 2024-09-07 04:01:29
//string concat with a macro
#define HTS_(i,j) hts_##i##_##j

//usage
//declare variables    
    TH1F* HTS_(5,53);
    TH1F* HTS_(5,54);
    //but note that the following produces variable hts_k_l, not hts_8_9;
    int k = 8;
    int l = 9;
    TH1F* HTS_(k,l);
    //use your variables
    hts_5_53 = HDir.make<TH1F>("hts_5_53")," Title", 100,0.,100.);
    hts_5_54 =HDir.make<TH1F>("hts_5_54")," Title", 100,0.,100.);

我认为 Boost.Preprocessor 能够在“循环”中使用它

但是我无法想象所有这些都可以应用于什么:)它通常是用数组完成的

//string concat with a macro
#define HTS_(i,j) hts_##i##_##j

//usage
//declare variables    
    TH1F* HTS_(5,53);
    TH1F* HTS_(5,54);
    //but note that the following produces variable hts_k_l, not hts_8_9;
    int k = 8;
    int l = 9;
    TH1F* HTS_(k,l);
    //use your variables
    hts_5_53 = HDir.make<TH1F>("hts_5_53")," Title", 100,0.,100.);
    hts_5_54 =HDir.make<TH1F>("hts_5_54")," Title", 100,0.,100.);

I think Boost.Preprocessor is able to use it in a "loop"

However I cannot imagine what all of this can be applied for :) It's done with arrays normally

随风而去 2024-09-07 04:01:28

您无法在代码中构造字符串,然后使用这些名称返回具有这些名称的变量。编译器在将程序构建为可执行代码时会丢弃变量名称。

您正在做的事情可能最好用数组来解决。定义一个具有所需维度的数组名称 hts。 C++ 数组始终从零开始索引,但最低界限似乎是 5。您可以在使用索引时从所有索引中减去 5 个元素,也可以将数组加长 5 个元素并“丢弃”较低的元素。

TH1F* hts[17][70];
for (int i = 5; i < 17; ++i) {
  for (int j = 53; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i << "_" << j;
    hts[i][j] = HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.);
  }
}

您的 make 行中存在语法错误;我没有尝试在这里修复它。

要拥有最小尺寸的数组,您必须在使用索引之前调整它们:

int const Offset1 = 5;
int const Offset2 = 53;
TH1F* hts[17-Offset1][70-Offset2];
for (int i = Offset1; i < 17; ++i) {
  for (int j = Offset2; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i - Offset1 << "_" << j - Offset2;
    hts[i][j] = HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.);
  }
}

另一个选择是使用从字符串到 TH1F 对象的 map

std::map<std::string, TH1F*> hts;
for (int i = 5; i < 17; ++i) {
  for (int j = 53; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i << "_" << j;
    hts.insert(name.str()), HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.));
  }
}

然后您可以使用以下方式访问您想要的任何项目:名称:

hts["hts_5_62"]->Fill(f);

You can't construct strings in code and then use those names to get back to the variables that had those names. The compiler throws away variables' names as it builds your program into executable code.

What you're doing is probably better solved with an array. Define an array name hts that has the dimensions you need. C++ arrays are always indexed from zero, but your lowest bound appears to be five. You can either subtract five from all your indices whenever you use them, or you can just make your array five elements longer and "throw away" the lower elements.

TH1F* hts[17][70];
for (int i = 5; i < 17; ++i) {
  for (int j = 53; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i << "_" << j;
    hts[i][j] = HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.);
  }
}

You have a syntax error somewhere in your make line; I have not attempted to fix it here.

To have a minimally sized array, you'll have to massage the indices before you use them:

int const Offset1 = 5;
int const Offset2 = 53;
TH1F* hts[17-Offset1][70-Offset2];
for (int i = Offset1; i < 17; ++i) {
  for (int j = Offset2; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i - Offset1 << "_" << j - Offset2;
    hts[i][j] = HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.);
  }
}

Another option is to use a map from strings to your TH1F objects:

std::map<std::string, TH1F*> hts;
for (int i = 5; i < 17; ++i) {
  for (int j = 53; j < 70; ++j) {
    ostringstream name;
    name << "hts_" << i << "_" << j;
    hts.insert(name.str()), HDir.make<TH1F>(name.str()), " Title", 100, 0., 100.));
  }
}

Then you can access any item you want using the name:

hts["hts_5_62"]->Fill(f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文