免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1622 | 回复: 6
打印 上一主题 下一主题

g++不支持string 数组么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-03 13:12 |只看该作者 |倒序浏览
#include <iostream>
#include <string>

using namespace std;

void pntstr(string *str)
{
    for(int i=0; i<4; i++)
        cout << str[i] << " ";
    cout << endl;
}

int main()
{
    string ss[] = {"aaa", "bbb", "ccc", "ddd"};
    pntstr();
}

程序编不过去
#include <iostream>
#include <string>

using namespace std;

void pntstr(string str[4])
{
    for(int i=0; i<4; i++)
        cout << str[i] << " ";
    cout << endl;
}

int main()
{
    string ss[] = {"aaa", "bbb", "ccc", "ddd"};
    pntstr();
}
也编不过去

论坛徽章:
0
2 [报告]
发表于 2006-11-03 13:26 |只看该作者
>>pntstr();

pntstr(ss);

论坛徽章:
0
3 [报告]
发表于 2006-11-03 13:34 |只看该作者
原帖由 converse 于 2006-11-3 13:26 发表
>>pntstr();

pntstr(ss);


一阵玄晕,哎,竟然。。。。

论坛徽章:
0
4 [报告]
发表于 2006-11-03 13:40 |只看该作者
#include <vector>
#include <iostream>
#include <string>

using namespace std;

void pntstr(vector<string> a)
{
    vector<string>::iterator pos = a.begin();
    for( ; pos != a.end(); ++pos)
        cout << *pos << " ";
    cout << endl;
}

int main()
{
    string ss[] = {"aaa", "bbb", "ccc", "ddd"};
    vector<string> v_ss;
    v_ss.assign(ss, ss+4);
    pntstr(v_ss);
}
这样也好了

[ 本帖最后由 chzht001 于 2006-11-3 13:50 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-11-03 13:52 |只看该作者
我就纳闷了你不会看编译器的提示?

论坛徽章:
0
6 [报告]
发表于 2006-11-03 13:55 |只看该作者
迭代器使用不对,
cout << a[pos] << " ";
=>
cout << *pos << " ";

论坛徽章:
0
7 [报告]
发表于 2006-11-03 14:14 |只看该作者
string ss[] = {"aaa", "bbb", "ccc", "ddd"};

不可以这样初始化吧 ?  据说c++ 0x可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP