简体   繁体   English

Gtk TextIter算法怎么做

[英]How to do Gtk TextIter arithmetic

I am attempting to use Gtk TextIter objects to lift three-character slices out of a TextBuffer, but am having trouble with the arithmetic. 我试图使用Gtk TextIter对象将三个字符的切片从TextBuffer中取出,但是在算术上遇到了麻烦。 I set up an iterator p to point to the start of the range and want q to point to three characters further on. 我设置了一个迭代器p来指向范围的开始,并希望q进一步指向三个字符。

I have tried... 我努力了...

q = p + 3;              // Doesn't compile
q = p; q += 3;          // Doesn't compile
q = p; q++; q++; q++;   // Happy

I'd like to know what the correct way to do this is. 我想知道正确的方法是。 The third method works, but looks like a ghastly hack. 第三种方法有效,但看起来像个骇人听闻的骇客。

Any thoughts? 有什么想法吗?

If you read the documentation you'll see that TextIter doesn't have a + or += operator. 如果阅读文档,您会发现TextIter没有++=运算符。 It's a bidirectional iterator and not a random access iterator so this is as it should be. 它是双向迭代器,而不是随机访问迭代器,因此应如此。

You can use either: 您可以使用以下任一方法:

q = p;
std::advance(q, 3);

or 要么

q = p;
q.forward_chars(3);

That's weird. 这很奇怪。

The underlying C API has gtk_text_iter_forward_chars() which should do the right thing. 底层的C API具有gtk_text_iter_forward_chars() ,它应该做正确的事情。 Maybe read the source, and/or file a bug on the C++ wrapper? 也许阅读源代码,和/或在C ++包装器上提交错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM