C++外部文件读取:我知道如何查找和读取字符串,findMe。但是我该如何处理 findMei(查找字符串中的任意数字 i)?

发布于 2024-10-20 14:46:23 字数 730 浏览 1 评论 0 原文

提前道歉,因为我怀疑这可能是一个愚蠢的问题。

我编写了一个用于从外部文件读取数据的函数。然后,我使用这些数据并使用我编写的其他代码执行计算。

该函数的工作原理是查找如下所示的数据标签:

 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.

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

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

发布评论

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

评论(3

卖梦商人 2024-10-27 14:46:23

很难理解你的意思,但我想你想要这个

#include <sstream>
#include <string>    

for (int i = 0; i < anyNumberOfDataSets; i++)  
{
    std::ostringstream strm;
    strm << "<dataLabel" << i << ">";
    const std::string findMe = strm.str();
    //...  
    //proceed with searching findMe     
}

你可以阅读有关字符串流的更多信息,例如, 这里

It is very hard to understand what you mean, but I guess you want this

#include <sstream>
#include <string>    

for (int i = 0; i < anyNumberOfDataSets; i++)  
{
    std::ostringstream strm;
    strm << "<dataLabel" << i << ">";
    const std::string findMe = strm.str();
    //...  
    //proceed with searching findMe     
}

You can read more about string streams, for instance, here

谈下烟灰 2024-10-27 14:46:23

你已经得到了正确的答案,所以这只是试图帮助你解决将来的此类问题:

你的核心问题是将整数 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 string s (if you've done this, than you just do findMe = "<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".

冷夜 2024-10-27 14:46:23

另一个解决方案:

using namespace boost;
findMe = str(format("<dataLabel%d>") % i);

这会将 %d 替换为 i 的值,格式类似于 printf()

Another solution:

using namespace boost;
findMe = str(format("<dataLabel%d>") % i);

This will substitute %d with the value of i, formatted like printf() does.

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