Chinaunix

标题: g++不支持string 数组么? [打印本页]

作者: chzht001    时间: 2006-11-03 13:12
标题: g++不支持string 数组么?
#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();
}
也编不过去
作者: converse    时间: 2006-11-03 13:26
>>pntstr();

pntstr(ss);
作者: chzht001    时间: 2006-11-03 13:34
原帖由 converse 于 2006-11-3 13:26 发表
>>pntstr();

pntstr(ss);


一阵玄晕,哎,竟然。。。。
作者: chzht001    时间: 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 编辑 ]
作者: converse    时间: 2006-11-03 13:52
我就纳闷了你不会看编译器的提示?
作者: dulao5    时间: 2006-11-03 13:55
迭代器使用不对,
cout << a[pos] << " ";
=>
cout << *pos << " ";
作者: rushrush    时间: 2006-11-03 14:14
string ss[] = {"aaa", "bbb", "ccc", "ddd"};

不可以这样初始化吧 ?  据说c++ 0x可以




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2