字符串数组的两种定义方式

时间:2015-05-15 10:37:35   收藏:0   阅读:167

C++实现字符串数组的两种方式

1、常用的方法

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str[]={"hello", "string", "world"};
    int str_size=sizeof(str)/sizeof(string);
    cout<<"size of str is "<<str_size<<endl;

    for (int i=0;i<str_size;++i)
        cout<<str[i]<<endl;
}

2.采用指针的方法

#include <iostream>
using namespace std;

int main()
{
    char *a[] = { "hello", "string", "world" };
    int a_size = sizeof(a) / sizeof(char *);
    cout << a_size << endl;

    for (int i = 0; i<a_size; i++)
        cout << *(a+i) << endl;

    return 0;
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!