标签:
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; }
标签:
原文地址:http://www.cnblogs.com/acode/p/4505195.html