提前道歉,因为我怀疑这可能是一个愚蠢的问题。
我编写了一个用于从外部文件读取数据的函数。然后,我使用这些数据并使用我编写的其他代码执行计算。
该函数的工作原理是查找如下所示的数据标签:
const std::string findMe = "<dataLabel>";
每次我想要查找数据时,我都会将 dataLabel 替换为我需要从文件中获取的任何数据的标签。
这就是我想做的。
我不想每次都在标签中写入我想要的数据。我希望能够做到这一点:
for (int i = 0; i < anyNumberOfDataSets; i++)
{
findMe = "<dataLabeli>";
// Then run function for reading in data, put data into a vector.
}
然后我可以将任意数量的数据集添加到我的外部文件中,为每个数据集指定标题
,并将每个数据集读入一个向量。
问题是,我根本不知道如何编写 findMe = ""
。这可能吗?
我尝试过诸如 findMe = ",但运气不好!
任何建议将不胜感激。
Apologies in advance, because I suspect this may be a silly question.
I have written a function for reading in data from an external file. I then use the data to perform calculations using other code I have written.
The function works by finding a data label that looks like this:
const std::string findMe = "<dataLabel>";
Each time I want to find data, I replace dataLabel with the label of whichever data I need from the file.
Here's what I want to do.
I don't want to have to write in the label of the data I want each time. I want to be able to do this:
for (int i = 0; i < anyNumberOfDataSets; i++)
{
findMe = "<dataLabeli>";
// Then run function for reading in data, put data into a vector.
}
I could then add any number of data sets to my external file, give each one the title
, and have each data set read into a vector.
The problem is, I simply can't figure out how to write findMe = "<dataLabeli>"
. Is this even possible?
I have tried things like, findMe = "<dataLabel" << i <<
, but no luck!
Any suggestions would be much appreciated.
发布评论
评论(3)
很难理解你的意思,但我想你想要这个
你可以阅读有关字符串流的更多信息,例如, 这里
It is very hard to understand what you mean, but I guess you want this
You can read more about string streams, for instance, here
你已经得到了正确的答案,所以这只是试图帮助你解决将来的此类问题:
你的核心问题是将整数
i
转换为字符串s< /code> (如果您已经这样做了,那么您只需执行
findMe = "。
谷歌搜索 c++ 将整数转换为字符串 会给你这个作为第一个结果,问题已解决。
这并不是说“先使用谷歌/而不是询问”,而是“尝试找出核心问题”。
you've already got the right answer, so this is just trying to help you with solving such problems in the future:
Your core problem here is to convert the integer
i
into a strings
(if you've done this, than you just dofindMe = "<datalabel"; findMe += s; findMe += ">";
.Googling for c++ convert integer into string will give you this as the first result. Problem solved.
This is not saying "use google before/instead of asking", it's rather "try to identify the core problem".
另一个解决方案:
这会将
%d
替换为i
的值,格式类似于printf()
。Another solution:
This will substitute
%d
with the value ofi
, formatted likeprintf()
does.