在循环内将字符串和运行索引连接到字符串中
要使用给定的图形包,我需要定义、预订和填充直方图。 如何获取直方图的名称,它是一个要连接的字符串 用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太明白你的问题。
为“书”声明变量后,您正在对它们做什么?如果将它们放入某种集合中,则可能不需要为每个集合声明一个变量。只需在内部 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.
标准 C++ 字符串流是字符串格式化的基本解决方案:
使用 os.str() 检索结果字符串。有关替代方案的讨论,请参阅“Manor farm 的字符串格式化程序”< /em>。
有了结果字符串和合适的容器,你的循环可能会变成:
为什么你'我不清楚是否需要首先将索引存储在字符串中。
旁注:如果您正在学习一本不涉及字符串格式化和容器合理使用的书,请选择一本好的入门书来自 SO 图书指南。
In standard C++ string streams are the basic solution for string formatting:
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:
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.
我认为 Boost.Preprocessor 能够在“循环”中使用它
但是我无法想象所有这些都可以应用于什么:)它通常是用数组完成的
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
您无法在代码中构造字符串,然后使用这些名称返回具有这些名称的变量。编译器在将程序构建为可执行代码时会丢弃变量名称。
您正在做的事情可能最好用数组来解决。定义一个具有所需维度的数组名称
hts
。 C++ 数组始终从零开始索引,但最低界限似乎是 5。您可以在使用索引时从所有索引中减去 5 个元素,也可以将数组加长 5 个元素并“丢弃”较低的元素。您的
make
行中存在语法错误;我没有尝试在这里修复它。要拥有最小尺寸的数组,您必须在使用索引之前调整它们:
另一个选择是使用从字符串到 TH1F 对象的
map
:然后您可以使用以下方式访问您想要的任何项目:名称:
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.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:
Another option is to use a
map
from strings to your TH1F objects:Then you can access any item you want using the name: