首页
社区
课程
招聘
[求助]C++如何取数组个数并提取每个数组值?
发表于: 2008-3-16 09:51 12353

[求助]C++如何取数组个数并提取每个数组值?

2008-3-16 09:51
12353
请大大们帮帮忙好吗?  当照顾一下小弟~~谢谢拉~

[课程]FART 脱壳王!加量不加价!FART作者讲授!

上传的附件:
  • c.jpg (28.65kb,89次下载)
收藏
免费 0
支持
分享
最新回复 (10)
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
2
int array[3] = {1,12,123};

printf("数组共有:%d个成员。\n",sizeof(array));
printf("第一个数组成员是:%d\n",array[0]);
printf("第二个数组成员是:%d\n",array[1]);
printf("第三个数组成员是:%d\n",array[2]);
2008-3-16 10:02
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
版版~还能请教一下吗?  谢谢小虾版版的热心帮助~



#include <stdio.h>

int i=0,array[6];

void main()
{
        for (i;i<6;i++)
        {
                array[i]=i;
        }
        printf("数组个数:%d\n",sizeof(array));
       
}

怎么求不出来这样子的效果???
上传的附件:
2008-3-16 10:25
0
雪    币: 2384
活跃值: (766)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
4
// 我忽略了sizeof是取字节数而不是取类型数的,下面的代码才是正确的。
#include <stdio.h>
#include <stdlib.h>

int main()
{
        int i,array[6];
        for (i = 0; i < sizeof(array) / sizeof(int); i++)
                array[i] = i + 1;
        printf("数组个数是:%d\n",sizeof(array) / sizeof(int));
        for (i = 0; i < sizeof(array) / sizeof(int); i++)
                 printf("数组成员%d是:%d\n",i+1,array[i]);
        system("pause");
        return 0;
}
2008-3-16 10:51
0
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
5
动态数组和静态数组是不一样的 LZ不要搞混了。
要使用动态Array 你可以考虑用MFC 的CArray类
当然 C++ STL的东西也可以的。
    看来地球上还有比我稍微菜一点的人。
2008-3-16 10:53
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
谢谢~两位大大~~~原来c++这么麻烦滴~
2008-3-16 10:56
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
这样子应该可以了吧~~~嘿嘿....  就是不知道怎么在外面取成员出来~

[QUOTE=]#include <stdio.h>

int i=0,a=0,*array;

void main()
{
        for (i;i<6;i++)
        {
                ++a;
                array=new int[i+1];
                array[i]=i;
                printf("当前数组%d值为:%d\n",i,array[i]);
        }
        printf("数组个数:%d\n",a/*sizeof(array)/sizeof(int)*/);
       

        delete array;
}[/QUOTE]
2008-3-16 11:17
0
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
8
// TutArrayTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <string>
#include <vector>

int _tmain(int argc, _TCHAR* argv[])
{
        printf("usage : \r\n"
                "type 'exit' to end the program \r\n"
                "type 'size' to list the elementnumber of the array \r\n"
                "type 'list' to list all elements within the array \r\n"
                "overwise add a new element in to the array \r\n");
        std::vector<std::string> strarray;
        for(;;)
        {
                std::string str;
                char buffer[1024];
                gets_s(buffer);
                str = buffer;
                if(str == "exit")
                        break;
                else if(str == "size"){
                        printf("%d \r\n",strarray.size());
                }else if(str == "list"){
                        for(unsigned int i = 0;i < strarray.size();i++){
                                printf("Index[%d] = %s\r\n",i,strarray[i].c_str());
                        }
                }else{
                        strarray.push_back(str);
                        puts("new element added");
                }
        }
        system("PAUSE");
        return 0;
}

我帮你写了一个示例代码 很好的演示了 数组的用法。其实不必易难多少 只是LZ对类库不熟悉罢了。
2008-3-16 12:06
0
雪    币: 100
活跃值: (11)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
哎,C++我还没学呢~!~
2008-3-16 12:20
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
谢谢foxabu大大~!~

c++6.0需要把
#include <stdio.h> 加上
gets_s  改成  gets

非常感谢了~~~~呵呵...高兴ing....
2008-3-16 12:36
0
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
练习题~~~~ 虽然没有错误.但为什么有那么多的警告哦...

[QUOTE=]#include <stdlib.h>
#include <windows.h>
#include <vector>
#include <stdafx.h>
#include <string.h>

//using spacename std;

std::vector<std::string> H_array;

BOOL CALLBACK EnumChildProc(
  HWND hwnd,      // handle to child window
  LPARAM lParam   // application-defined value
  )
{
        std::string str;
        char buffer[1024];
        if(hwnd>0)
        {
                GetWindowText(hwnd,buffer,255);
                str=(std::string)buffer;

                H_array.push_back(str);

                return TRUE;
        }else
        {
                return FALSE;
        }
               

}

int main(int argc,char argv[])
{

        printf("正在枚举窗口列表...\n");
        EnumChildWindows(0,(WNDENUMPROC)EnumChildProc,NULL);
        for (unsigned int i=0;i<H_array.size();i++)
        {
                printf("%d:   %s\n",i,H_array[i].c_str());
        }

        return 0;
}

[/QUOTE]

--------------------Configuration: EnumChildWindows - Win32 Debug--------------------
Compiling...
EnumChildWindows.cpp
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Linking...

EnumChildWindows.exe - 0 error(s), 4 warning(s)
2008-3-16 13:30
0
游客
登录 | 注册 方可回帖
返回
//