请问用vector作返回值的函数应该怎样写 请问用vector作返回值的函数应该怎样写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #include <iostream>#include <vector>#include <algorithm>using namespace std;vector<int> ret_vecInt(){ vector<int> vecInt(5, 10); return vecInt;}void main(){ vector<int> vecIntTemp(5, 10); vecIntTemp = ret_vecInt(); copy(vecIntTemp.begin(), vecIntTemp.end(), ostream_iterator<int>(cout, "\n"));} #include <cstdlib>#include <cstring>#include <string>#include <vector>#include <iostream>using namespace std;typedef vector<string> vector_str;vector_str * split_str(const char *str, const char *splitter){ vector_str * p_vector = new vector_str; char * token; char * buff = new char[strlen(str) + 1]; strcpy(buff, str); token = strtok(buff, splitter); while (token != NULL) { p_vector->push_back(token); token = strtok(NULL, splitter); } delete[] buff; return p_vector;}int main(int argc, char *argv[]){ vector_str * v1 = split_str("aa:bb:cc:dd", ":"); vector_str::iterator Iter; for (Iter = v1->begin(); Iter != v1->end(); Iter++) { cout << *Iter << ' '; } delete v1; system("PAUSE"); return 0;} 返回指针需要在外面释放的,如果调用者不是写这个函数的人容易忘记释放比较好的做法是传vector 引用进入函数, VS2010 (VC++)无法注册插件,请问如何处理! 怎么更改CDC基类函数? Vistual Studio 2005 的控件除了基本的,其它的都是灰的,不能用 关于读写Outlook Express通讯录的问题 在绘图程序中如何清空屏幕工作区域? 2个对话框,如何取得其中任何一个对话框中控件的指针 关于自定义资源的使用?有DX指点指点 如何在导出"类中带有虚函数"的类 串口通讯 CSpinButtonCtrl 50分急切求教:如何用自己编的小键盘,实现对不同文本框的输入 用sendmessage()发送数据,第一次成功,以后都失败,为什么?
#include <vector>
#include <algorithm>
using namespace std;
vector<int> ret_vecInt()
{
vector<int> vecInt(5, 10);
return vecInt;
}void main()
{
vector<int> vecIntTemp(5, 10);
vecIntTemp = ret_vecInt();
copy(vecIntTemp.begin(), vecIntTemp.end(), ostream_iterator<int>(cout, "\n"));
}
#include <cstring>
#include <string>
#include <vector>
#include <iostream>using namespace std;typedef vector<string> vector_str;vector_str * split_str(const char *str, const char *splitter)
{
vector_str * p_vector = new vector_str;
char * token;
char * buff = new char[strlen(str) + 1];
strcpy(buff, str);
token = strtok(buff, splitter);
while (token != NULL) {
p_vector->push_back(token);
token = strtok(NULL, splitter);
}
delete[] buff;
return p_vector;
}int main(int argc, char *argv[])
{
vector_str * v1 = split_str("aa:bb:cc:dd", ":");
vector_str::iterator Iter;
for (Iter = v1->begin(); Iter != v1->end(); Iter++) {
cout << *Iter << ' ';
}
delete v1;
system("PAUSE");
return 0;
}
比较好的做法是传vector 引用进入函数,