为什么我不能输出vector的元素? [英] Why can't I output vector's elements ?

查看:101
本文介绍了为什么我不能输出vector的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我想首先感谢所有花时间查看此主题并尝试提供帮助的人。





我有一个类型为

Hello everyone!

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.


I have a global static variable of type

static wchar_t d_sondi[50]

的全局静态变量,以及全局静态向量定义为



, and a global static vector defined as

static vector<wchar_t*> vDubinaSonde;

。在我的GUI中,添加了一个按钮,该按钮应该使用编辑控件中的文本填充矢量。



通过检查向量的大小,我确定填充工作正常,但字符串输出不好。



当我输出向量内容时,它输出最后一个条目,而不是所有元素。



使用GetDlgItemText();



这是按钮控制的处理程序:



. In my GUI, a button was added that should fill vector with text from edit control.

By checking the size of the vector, I have determined that fill works fine, but output of strings is not good.

When I output vectors content, it outputs last entry, instead of all elements.

Text is retreived by using GetDlgItemText();

Here is the handler for button control:

case IDC_BUTTON9:
                {
                    if( tip_gte == L"'Литогеотермална енергија'")
                    {
                        memset( &n_sondi, '\0', sizeof(n_sondi) );

                        GetDlgItemText( hwnd, IDC_EDIT15, n_sondi, 50 );

                        if( !wcslen(n_sondi) )
                            wsprintf( n_sondi, L"%s", L"0" );

                        memset( &d_sondi, '\0', sizeof(d_sondi) );

                        GetDlgItemText( hwnd, IDC_EDIT16, d_sondi, 50 );

                        if( !wcslen(d_sondi) )
                            wsprintf( d_sondi, L"%s", L"0" );

                        vBrojSonde.push_back(n_sondi);

                        vDubinaSonde.push_back(d_sondi);

                        for( vector<wchar_t*>::size_type in = 0;
                            in < vDubinaSonde.size(); in ++ )
                            MessageBox( hwnd, vDubinaSonde[in], L"", MB_OK );

                    }
                }

                break;





我在控制台中试过这个代码,它的工作方式应该是这样的:





I have tried this code in console, and it works the way it should :

#include <iostream>
#include <vector>

using std::vector;
using std::endl;
using std::wcout;

int main()
{
	vector<wchar_t*> v;

	wchar_t test[4] = L"123";

	v.push_back(test);
	v.push_back(test);
	v.push_back(test);

	for( vector<wchar_t*>::size_type i = 0; i < v.size(); i++ )
		wcout << v[i] << endl;

	return 0;
}





那么为什么它只显示向量中的最后一个值,在上面的按钮处理程序代码中呢? br />


我使用纯WIN32 API在MS Visual Studio Express 2008,Windows XP,C ++中工作。



如果需要任何其他信息(源代码或类似的东西),请求它,我会非常乐意提供它。



So why does it display only the last value in the vector, in the above code for button handler?

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using pure WIN32 API.

If any other information is required ( source code or something similar ), please ask for it, I will more than gladly supply it.

推荐答案

我认为代码正在做它应该做的事情。



你正在一次又一次地将全局变量d_sondi推向矢量。所以基本上你的向量只包含同一个变量的多个副本(d_sondi)。由于每次单击按钮都要更新全局变量,向量将反映最后一个值。



我希望它有所帮助。
I think the code is doing what it should do.

You are pushing a global variable d_sondi to vector again and again. So basically your vector will just contain multiple copies of the same variable(d_sondi). And since you are updating the global variable everytime you click the button, vector is reflecting the last value.

I hope it helps.


好的,我的CPP有点生疏,但这里有一些想法让你去...



首先,你的字符串应该是(我认为):

OK, my CPP is a little rusty, but here's some ideas to get you going...

First, your string should be (I think):
wchar_t* test = L"123";





你遇到的问题是因为s​​td: :vector不支持数组作为内部类型。您可以在固定大小字符串的结构中创建自己的类型并以这种方式使用它,但由于处理数组的方式,这不受支持。



The problem you are having though is because std::vector does not support arrays as the inner type. You can create your own type in a structure for fixed size strings and use it that way, but because of the way arrays are handled this isn't supported.


使用 wstring 而不是 wchar_t 数组。

所以你需要一个 wstring 向量< wstring>< / wstring>


这篇关于为什么我不能输出vector的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆