查看完整版本: 借问一下,关于C++中函数返回值的问题

Zerk 2007-12-18 03:45 PM

借问一下,关于C++中函数返回值的问题

如果我希望我的函数返回一个vector<int>型值,并把这个值赋给变量a,程序应该怎么写呢?

imac 2007-12-18 04:35 PM

vector<T> func()
{
    vector<T> r = new vector<T>();
    return r;
}

vector<T> a;
a = func();

new是new在heap上,不用想太多了,不会出函数就清空的。

Zerk 2007-12-18 04:42 PM

[quote]原帖由 [i]imac[/i] 于 2007-12-18 03:35 PM 发表
vector<T> func()
{
    vector<T> r = new vector<T>();
    return r;
}

vector<T> a;
a = func();

new是new在heap上,不用想太多了,不会出函数就清空的。 [/quote]

我试过了,code是这样写的:

vector&lt;string&gt; func(){
      vector&lt;string&gt; r=new vector&lt;string&gt;();
      return r;
}

可是提示错误说:

error: 'initializing' : cannot convert from 'class std::vector<class std::basic... ...
No constructor could take the source type, or constructor overload resolution was ambiguous

我也不知道这是为什么

[[i] 本帖最后由 Zerk 于 2007-12-18 03:46 PM 编辑 [/i]]

kknd 2007-12-18 05:09 PM

是不是应该:
vector<string>* func(){
vector<string>* r=new vector<string>();
return r;
}

or

vector<string> func(){
vector<string> r; // 不用new动态分配
return r;
}

Zerk 2007-12-18 05:13 PM

[quote]原帖由 [i]kknd[/i] 于 2007-12-18 04:09 PM 发表
是不是应该:
vector<string>* func(){
vector<string>* r=new vector<string>();
return r;
}

or

vector<string> func(){
vector<string> r; // 不用new动态分配
re ... [/quote]


Great, it works, but seems the function declaration I got from others is just:

vector&lt;string&gt; func(){};

anyway, thx very much

kknd 2007-12-18 05:24 PM

vector* func(){
vector* r=new vector();
return r;
}
是动态分配,用完以后应该要delete:
vector* a = func();  // a是指针,指向func里面new的那个vector
...
delete a;

vector func(){
vector r; // 不用new动态分配
return r;
是静态分配,不用delete:
vector a = func(); // a 不是指针,是func里面那个vector的拷贝。func里面那个vector出了func就自动释放了

如果数组很大最好用第一种方法。

imac 2007-12-18 05:31 PM

你要用vector< string >就不一样了吧...

non-scalar type...

我刚才写的是vector< int >,但是< >里面的被系统自动忽略了 :(

我也想迷糊了,vector是没有non-parameter cstor的... 动态分配吧

[[i] 本帖最后由 imac 于 2007-12-18 04:34 PM 编辑 [/i]]

chao 2007-12-18 05:32 PM

[quote]原帖由 [i]Zerk[/i] 于 2007-12-18 05:13 PM 发表



Great, it works, but seems the function declaration I got from others is just:

vector&lt;string&gt; func(){};

anyway, thx very much [/quote]

If the declaration is vector&lt;string&gt; func();
then it should be:

vector&lt;string&gt; func(){
vector&lt;string&gt; r;

return r;
}

chao 2007-12-18 05:34 PM

[quote]原帖由 [i]Zerk[/i] 于 2007-12-18 04:42 PM 发表


我试过了,code是这样写的:

vector&lt;string&gt; func(){
      vector&lt;string&gt; r=new vector&lt;string&gt;();
      return r;
}

可是提示错误说:

error: 'i ... [/quote]
if you use new then the return type needs to be vector&lt;string&gt; *

Zerk 2007-12-18 06:09 PM

[quote]原帖由 [i]chao[/i] 于 2007-12-18 04:32 PM 发表


If the declaration is vector&lt;string&gt; func();
then it should be:

vector&lt;string&gt; func(){
vector&lt;string&gt; r;

return r;
} [/quote]


hehe, thanks a lot, actually my question is, could I assign a value to "A" in this way:


vector&lt;string&gt; func(){} //function declaration;
string &lt;string&gt;  A; //without initialization;
A=func();//seems this doesn't work.

chao 2007-12-19 01:49 AM

[quote]原帖由 [i]Zerk[/i] 于 2007-12-18 06:09 PM 发表



hehe, thanks a lot, actually my question is, could I assign a value to "A" in this way:


vector&lt;string&gt; func(){} //function declaration;
string &lt;string& ... [/quote]
work啊。。。咋不work,只要你func写对了话
页: [1]
查看完整版本: 借问一下,关于C++中函数返回值的问题