English 中文(简体)
如何在C++中管理stream子?
原标题:How to manage ostringstream object in C++?

这是C++方案的一项编码。

string TwoSeries::getArrays()
{
    ostringstream outIndex;
    ostringstream outValueA;
    ostringstream outValueB;
    string stA;
    string stB;
    string valueA;
    string index;
    int *arA;
    int * arB;
    string valueB;
    for(int x = 0; x < 200; x++)  
    {         

        outIndex << x;
        index = outIndex.str();



         arA = getArrayA();
        outValueA << *(arA + x);
        valueA = outValueA.str();


          arB = getArrayB();
        outValueB << *(arB + x);
        valueB = outValueB.str();


        stA += index + ":" + valueA + " ";
        stB += index + ":" + valueB + " ";

    }

   // return "Series A: 
"+stA+ "
"+"Series B: 
"+ stB;   
    return index;
}

这一职能应当将最后的指数从隐形转化为扼杀,这应当是199。 但是,该标的是Index ,它把所有数字(指示数)加在一起,并因此得出类似结果:1234567891011121314151617. 198199。 最后的数字是199。 在完全停机后,只用最后数字来强制履行这一职能,而其所有数字都是如此。 如何做到这一点?

最佳回答

你们要明确:

for(int x = 0; x < 200; x++)  
{         
    outIndex.str("");
    outValueA.str("");
    outValueB.str("");

或者,你可以采用良好的C++风格,在当地宣布:

for(int x = 0; x < 200; x++)  
{         
    ostringstream outIndex;
    ostringstream outValueA;
    ostringstream outValueB;

在你回头的时候,你也可以把其他人移走。 或... 改写如下:

string TwoSeries::getArrays()
{
    string index;

    int x;
    for(x = 0; x < 200; x++)  
    {         
        ostringstream osA, osB;

        osA << x << ":" << *(getArrayA() + x) + " ";
        osB << x << ":" << *(getArrayB() + x) + " ";

        string stA = osA.str(); // warning: value isn t used
        string stB = osB.str(); // warning: value isn t used
    }

    ostringstream osA, osB;
    outIndex << (x-1); // previous index
    return outIndex.str();
}

请注意,你做了大量多余的工作,现在所有这些价值都没有得到利用。 也许,你有更多的密码显示:

问题回答

只剩下胎体所需的物体。 这使得它们重新定位:

string TwoSeries::getArrays()
{
    string stA;
    string stB;
    for(int x = 0; x < 200; x++)  
    {
        ostringstream outIndex;  //this stream used all three times. 
        outIndex << x;
        string index = outIndex.str();

        int *arA;
        arA = getArrayA();
        outIndex << *(arA + x);
        string valueA = outIndex.str();

        int * arB;
        arB = getArrayB();
        outIndex << *(arB + x);
        string valueB = outIndex.str();

        stA += index + ":" + valueA + " ";
        stB += index + ":" + valueB + " ";
    }

   return "Series A: 
"+stA+ "
"+"Series B: 
"+ stB;   
}

您的问题是,你将指数添加到<代码>outIndex上,但从来不重新编排,从而导致它慢慢地编制所有指数清单。 这对你的另外两条支柱也是这样。 <代码>.str(> > > > >> >> 系指流体。

for(int x = 0; x < 200; x++)  
{         
    outIndex << x;
}

将会持续地聚集到Index。 我认为,你需要做以下工作:

for(int x = 0; x < 200; x++)  
{         
    outIndex << x;

    ....

    outIndex.str("");
}

每次通过该休息室清除Index。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签