简体   繁体   English

在stringstream对象中获取字符串的地址

[英]Getting address of string in stringstream object

Is it possible to get the pointer to the string stored in string stream object . 是否有可能获得指向存储在字符串流对象中的字符串的指针。 Basically i want to copy the string from that location into another buffer .. I found that i can get the length from below code 基本上我想将字符串从该位置复制到另一个缓冲区..我发现我可以从下面的代码中获取长度

 myStringStreamObj.seekg(0, ios::end);
 lengthForStringInmyStringStreamObj = myStringStreamObj.tellg();

I know i can always do myStringStreamObj.str().c_str() . 我知道我总是可以做myStringStreamObj.str().c_str() However my profiler tells me this code is taking time and i want to avoid it . 但是我的探查器告诉我这段代码需要花费时间,我想避免使用它。 hence i need some good alternative to get pointer to that string. 因此,我需要一些不错的选择来获取指向该字符串的指针。

My profiler also tells me that another part of code where is do myStringStreamObj.str(std::string()) is slow too . 我的探查器还告诉我, myStringStreamObj.str(std::string())另一部分代码也很慢。 Can some one guide me on this too . 也有人可以指导我。

Please , I cant avoid stringstream as its part of a big code which i cant change / dont have permission to change . 请,我不能避免将stringstream作为我无法更改/未经许可的大代码的一部分。

The answer is "no". 答案是不”。 The only documented API to obtain the formatted string contents is the str () method. 唯一记录格式的字符串内容的API是str ()方法。

Of course, there's always a small possibility that whatever the compiler or platform you're using might have its own specific non-standard and/or non-documented methods for accessing the internals of a stringstream object; 当然,无论您使用的编译器或平台如何,都有很小的可能性可能会有其自己的特定非标准和/或非文档化方法来访问stringstream对象的内部。 which might be faster. 可能更快。 Because your question did not specify any particular compiler or implementation, I must conclude that you are looking for a portable, standards-compliant answer; 因为您的问题未指定任何特定的编译器或实现,所以我必须得出结论,您正在寻找一种可移植的,符合标准的答案; so the answer in that case is a pretty much a "no". 因此,在这种情况下,答案几乎是“否”。

I would actually be surprised if any particular compiler or platform, even some who might have a "reputation" for poisonings language standards <cough> , would offer any alternatives. 如果任何特定的编译器或平台,甚至可能对中毒语言标准<cough>有“声誉”的某些编译器或平台提供任何替代方案,我真的会感到惊讶。 I would expect that all implementations would prefer to keep the internal stringstream moving gears private, so that they can be tweaked and fiddled with, in future releases, without breaking binary ABI compatibility. 我希望所有实现都希望保留内部stringstream移动设备的私有性,以便在将来的发行版中对它们进行调整和修改,而不会破坏二进制ABI兼容性。

The only other possibility you might want to investigate is to obtain the contents of the stringstream using its iterators. 您可能要研究的唯一其他可能性是使用其迭代器获得stringstream的内容。 This is, of course, an entirely different mechanism for pulling out what's in a stringstream ; 当然,这是一种完全不同的机制来提取stringstream and is not as straightforward as just calling one method that hands you the string, on a silver platter; 并不像在银盘上调用一个将字符串交给您的方法那样简单; so it's likely to involve a fairly significant rewrite of your code that works with the returned string. 因此,很可能涉及对与返回的字符串一起使用的代码进行相当大的重写。 But it is possible that iterating over what's in the stringstream might turn out to be faster since, presumably, there will not be any need for the implementation to allocate a new std::string instance just for str ()'s benefit, and jamming everything inside it. 但它有可能是遍历什么的stringstream可能变成是更快,因为,大概不会有任何需要实施分配一个新std::string实例只是str ()的利益,并干扰里面的一切。

Whether or not iterating will be faster in your case depends on how your implementation's stringstream works, and how efficient the compiler is. 在您的情况下,迭代是否会更快取决于实现的stringstream工作方式以及编译器的效率。 The only way to find out is to go ahead and do it, then profile the results. 找出答案的唯一方法是继续进行,然后分析结果。

I can not provide a portable, standards compliant method. 我无法提供一种可移植的,符合标准的方法。

Although you can't get the internal buffer you can provide your own. 尽管您无法获得内部缓冲区,但是可以提供自己的缓冲区。

According to the standard setting the internal buffer of a std::stringbuf object has implementation defined behaviour. 根据标准设置, std::stringbuf对象的内部缓冲区具有实现定义的行为。

cplusplus.com: std::stringbuf::setbuf() cplusplus.com:std :: stringbuf :: setbuf()

As it happens in my implementation of GCC 4.8.2 the behaviour is to use the external buffer you provide instead if its internal std::string . 正如我在GCC 4.8.2的实现中所发生的那样,其行为是使用您提供的外部缓冲区代替内部的std::string

So you can do this: 因此,您可以执行以下操作:

int main()
{
    std::ostringstream oss;

    char buf[1024]; // this is where the data ends up when you write to oss
    oss.rdbuf()->pubsetbuf(buf, sizeof(buf));

    oss << "Testing" << 1 << 2 << 3.2 << '\0';

    std::cout << buf; // see the data
}

But I strongly advise that you only do stuff like this as a (very) temporary measure while you sort out something more efficient that is portable according to the standard. 但是,我强烈建议您在做一些符合标准的可移植性更高效率的事情时,只做这样的事情(非常临时的措施)。

Having said all that I looked at how my implementation implements std::stringstream::str() and it basically returns its internal std::string so you get direct access and with optimization turned on the function calls should be completely optimized away. 说了这么多,我就了解了我的实现是如何实现std::stringstream::str() ,它基本上返回了其内部std::string因此您可以直接访问,并在打开优化的情况下应该完全优化函数调用。 So this should be the preferred method. 因此,这应该是首选方法。

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

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