问题标签 [istringstream]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
234 浏览

c++ - istringsteam 带换行符

好的,我读到如果我们有一个字符串 s = 1 2 3"

我们可以做的 :

假设我们有一个包含以下内容的文本文件:

测试
1 100 毫秒

测试
2 200 毫秒

测试
3 300 毫秒

0 投票
0 回答
117 浏览

c++ - 使用字符串流将字符串解析为多个部分

我有一个输入文件,其中包含当前格式的多行:

[消息1]/[消息2]/[消息3]

我必须提取 [message#] 并将每个放入一个单独的容器(向量、数组等)。有没有一种简单的方法可以用字符串流做到这一点?

注意:[message#] 将不包含字符“/”。

我最初尝试这样做:

但这行不通。任何提示表示赞赏。

0 投票
1 回答
1008 浏览

c++ - 用于读取双精度的 istream 操纵器

我需要14.3925125E从 istringstream 中读取数字作为格式化输入的一部分。E意思是东方,不是科学记数法。
当我尝试使用input >> double >> char它时,看起来流将数字视为错误的科学并且失败了。我尝试了机械手std::fixedstd::dec但没有帮助。还有什么我可以用的吗?

0 投票
3 回答
528 浏览

c++ - 使用 QFile 将 istringstream 作为二进制输入(用于 libpng)

我正在尝试使用 libpng 从 Qt 资源中读取 png。问题:进行读取的类应该具有 Qt 的任何依赖项。

第一步,阅读http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/#CustomRead我已经成功编写了一个函数

我还成功通过了一个普通的旧 ifstream

read_png(..)并让它成功读取 png。但是如何从 Qt 资源中获取(最好是独立于平台的)istream?性能不是什么大问题,所以我最初想出了

这实际上产生了一个问题,即乍一看,看起来不错,当说

我明白了

不过,似乎存在一些空白问题。为了

产量

虽然后者有效,但前者的变化最终导致 libpng 检查函数png_sig_cmp(..)拒绝我的 png 无效。我的第一个反应是关于“二进制”。然而:

  1. istringstream iss(std::stringstream::in | std::stringstream::binary); 感觉不错。
  2. QIODevice::ReadOnly 似乎没有二进制伙伴。

你看到我错过了什么吗?

0 投票
1 回答
35 浏览

c++ - 不必要的空格跳过

我正在使用它来读取输入:

" #text Markup used in this document is compatible with "没有引号的输入行看起来像这样

我想要实现的是在我的代码变量数据将包含"Markup used in this document is compatible with "

但是Markup即使在我指定我不希望它跳过空格之后,这段代码也会忽略单词之后的所有内容noskipws

0 投票
1 回答
45 浏览

c++ - 重新设置 istringstream 对象

当我运行此代码时:

istreamstream (iss) 在每次迭代时都保持不变。(该文件在程序开始时被读入向量。是的,我检查以确保数组具有相应的数据。)

换句话说,我得到这个输出:

而文件/向量实际上说:

我尝试了各种痕迹,无法查明问题所在。谢谢!

0 投票
1 回答
104 浏览

c++ - Trouble with Out of range in memory using stringsteam

I just started using the stringstream for the first time and I love the concept, but I am having a hard time finding where exactly I am having an out of range in memory with my stringstream function.

What my function does is it takes in a string, for example, "N02550 G3 X16.7379 Y51.7040 R0.0115" This is machine code for a CNC machine at my job. I pass the string to a stringstream in order to find the strings that have a X, Z, Y next to them, these are coordinates. It then gets rid of the character at the beggining in order to save the float number to my struct "Coordinate"(there are 3 doubles, x, y, z).

When I run a text file that has this machine code with 33 lines, my program works. When I run it with machine code of 718 lines, it gets to 718, then crashes with out of range memory. Then another weird part is when I run machine code with 118,000 lines, it goes up to around 22,000 lines then crashes. So I'm having trouble figuring out why it is able to do that and whats causing the problem.

Here is the function:

If you want to see the whole application(the application is well documented) then ask or if you need the txt files containing the machine code. Thank you!

0 投票
1 回答
1278 浏览

c++ - C ++ - 从标准输入中获取可变数量的整数,n次

我无法弄清楚为什么以下程序中的循环没有完全运行 testCount 次。请帮助使其正确。

0 投票
1 回答
343 浏览

c++ - istringstrem 类中的 peek() 行为

我看到了很多关于 peek 方法的问题,但我的话题几乎是显而易见的,但(我认为)很有趣。

假设您有一个二进制文件要读取,并且您选择在程序内存中将其作为一个整体调出并使用 istringstream 对象来执行读取。

例如,如果您正在搜索流中给定字节的位置,重复访问硬盘会浪费时间和资源......

但是,一旦您创建了 istringstream 对象,任何最终的 NULL 字节都会被视为 EOF 信号。

至少这是在以下短代码中发生在我身上的事情:

此代码不适用于 g++ 4.9 和 clang 3.5,假设在可以找到buffer匹配之前内部有一个空字节,因为该空字节设置了该位。sampleeof

所以我的问题是:这种方法是完全要避免的,还是有某种方法可以教导peek空字节不是“必然”流的结尾?

0 投票
3 回答
1338 浏览

c++ - CPP:解析字符串流太慢

我的 cpp 代码需要读取由空格分隔的浮点值组成的 7 MB 文本文件。将字符串值解析为浮点数组大约需要 6 秒,这对我的用例来说太多了。

我一直在网上查,人们说通常是物理 IO 需要时间。为了消除这种情况,我一次性将文件读入字符串流,并将其用于浮点解析。代码速度仍然没有提高。任何想法如何让它运行得更快?

这是我的代码(为简单起见,用 dummy_f 替换了数组条目):

编辑:

这是 test_cases.txt 文件的链接https://drive.google.com/file/d/0BzHKbgLzf282N0NBamZ1VW5QeFE/view?usp=sharing

使用此文件运行时,请将内循环尺寸更改为 128(打错了)

编辑:找到了一种让它工作的方法。将 dummy_f 声明为字符串并从字符串流中读取为字符串单词。然后使用 atof 将字符串转换为浮点数。花费的时间是 0.4 秒,这对我来说已经足够了。