如何传递STL容器(比如矢量)? [英] How to pass STL containers (say a vector) ?

查看:86
本文介绍了如何传递STL容器(比如矢量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,


我很长一段时间后回到C ++,我有这个简单的问题:如何使用一个STL容器? br />
喜欢说矢量或地图(往返于某个函数)?


函数:


vector<串GT; tokenize(string s){


vector< string> myvector;

//将s和push_back分割成myvector;


//这样可以吗?从funcion退出时向量被破坏了吗?

返回myvector;

}


main:

vector< ;串GT;结果= tokenize(s);


为了使它工作,必须将

函数(myvector)中向量的结果深入复制到"导致" myvector被销毁之前的矢量。

这是怎么回事?如果要从容器中复制大量的数据,这可能效率低吗?


在这种情况下,我应该使用指针吗?或者最有可能说auto_ptr到

容器?如下所示:


功能:


auto_ptr< vector< string>> tokenize(string s){


auto_ptr< vector< string> > myvector(new vector< string>);

// split s和push_back into(* myvector)push_back(xx);


//现在返回auto_ptr

返回myvector;

}


main:

vector< string>结果= tokenize(s);


或者这是一个矫枉过正(甚至可能是不正确的)。


我读过有关传递迭代器的内容代替。你会怎么做两个带有迭代器的



任何帮助都会受到赞赏。


谢谢你,


-Sanjay Kumar

Folks,

I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?

function:

vector<string> tokenize(string s){

vector<string> myvector;
//split s and push_back into myvector;

//is this ok ? vector destroyed on exit from funcion ?
return myvector;
}

main:
vector<string> result = tokenize(s);

For it to work, there has to be deep copy of the result of vector inside
function (myvector) into the "result" vector before myvector is destroyed.
Is that how it works ? Could this be inefficient if there is large amount
of data to be copied from the container ?

In that case should I user pointers ? Or most likely say auto_ptr to the
Container ? Like below:

function:

auto_ptr<vector<string>> tokenize(string s){

auto_ptr <vector<string> > myvector(new vector<string>);
//split s and push_back into (*myvector)push_back(xx);

//now return auto_ptr
return myvector;
}

main:
vector<string> result = tokenize(s);

Or is this an overkill (and may be even incorrect).

I have read about passing iterators instead. How would you do about two
with iterators ?

Any help would be appreciated.

thanks you,

-Sanjay Kumar

推荐答案



Sanjay Kumar写道:

Sanjay Kumar wrote:
伙计们,

很长一段时间后我又回到了C ++中,我有这个简单的问题:如何把一个STL容器搞砸
矢量或地图(往返于函数)?


更喜欢按值返回并通过const引用传递。

函数:

vector< string> tokenize(string s){
vector< string> tokenize(string const& s){
vector< string> myvector;
//将s和push_back分成myvector;

//这样可以吗?在退出函数时被摧毁的向量?
返回myvector;
}
主要:
vector< string> result = tokenize(s);

为了使它工作,必须将
函数(myvector)中vector的结果深层复制到result中。在myvector被摧毁之前的矢量。
它是如何工作的?
很可能不是(至少在非调试版本中)。 RVO(google for that

one)将启动并删除冗余副本。对于我知道的所有现代(2000或更高版本)编译器来说,这是

的情况。如果要从容器中复制大量数据,这可能效率低下吗?


如果您的编译器无法优化(我怀疑)。如果它不能
并且你花了很多时间返回你的容器,通过引用传递

返回值并用交换结束而不是返回:


void tokenize(string const& s,vector< string>& result){


vector< string> myvector;

//将s和push_back分割成myvector;


//这样可以吗?从funcion退出时向量被破坏了吗?

std :: swap(结果,myvector);

返回;

}


请注意,此功能现在不是那么容易使用。此外,它最可能会比原来的功能稍微慢一点。

在这种情况下,我应该使用指针吗?或者最有可能说auto_ptr到
容器?如下所示:
Never!
功能:

auto_ptr< vector< string>> tokenize(string s){

auto_ptr< vector< string> > myvector(new vector< string>);
//将s和push_back分成(* myvector)push_back(xx);

//现在返回auto_ptr
返回myvector;
}

main:
vector< string> result = tokenize(s);

或者这是一种矫枉过正(甚至可能是不正确的)。
这甚至不是合法的C ++ - 你将std :: auto_ptr分配给

std :: vector-
我已经阅读了关于传递迭代器的内容。你会如何处理两个带迭代器的东西?


迭代器对于将范围传递给函数非常有用。它不比通过常量参考传递容器快得多b $ b,但如果你不想传递整个容器,它会更加可靠。

当你不想返回一个

容器时,迭代器也很有用,但是例如追加一些价值观。现在,我建议你坚持使用容器。


/ Peter
任何帮助都将不胜感激。
谢谢你,

-Sanjay Kumar
Folks,

I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?
Prefer to return by value and pass by const reference.

function:

vector<string> tokenize(string s){ vector<string> tokenize(string const& s){
vector<string> myvector;
//split s and push_back into myvector;

//is this ok ? vector destroyed on exit from funcion ?
return myvector;
}

main:
vector<string> result = tokenize(s);

For it to work, there has to be deep copy of the result of vector inside
function (myvector) into the "result" vector before myvector is destroyed.
Is that how it works? Most likely not (at least in a non-debug build). RVO (google for that
one) will kick in and remove the redundant copy. This is the case for
all modern (2000 or later) compilers I know. Could this be inefficient if there is large amount
of data to be copied from the container ?
It could if your compiler cant optimise (which I doubt). If it can''t
and you spend to much time returning your container, pass the
returnvalue by reference and finish with a swap instead of the return:

void tokenize(string const& s,vector<string>& result){

vector<string> myvector;
//split s and push_back into myvector;

//is this ok ? vector destroyed on exit from funcion ?
std::swap(result,myvector);
return;
}

Notice that the function now is not so easy to use. Also, it will most
likely be slightly slower than the original function.

In that case should I user pointers ? Or most likely say auto_ptr to the
Container ? Like below: Never!
function:

auto_ptr<vector<string>> tokenize(string s){

auto_ptr <vector<string> > myvector(new vector<string>);
//split s and push_back into (*myvector)push_back(xx);

//now return auto_ptr
return myvector;
}

main:
vector<string> result = tokenize(s);

Or is this an overkill (and may be even incorrect). This isnt even legal C++ - you are assigning a std::auto_ptr to a
std::vector-
I have read about passing iterators instead. How would you do about two
with iterators ?
Iterators can be useful for passing ranges to a function. It is not
faster than passing the container by constant reference, but it is more
flesible in case you do not want to pass an entire container.
Iterators can also be useful when you do not want to return a
container, but rather would e.g. append some values. For now, I
recommend that you stick to containers.

/Peter
Any help would be appreciated.

thanks you,

-Sanjay Kumar








peter koch写道:

peter koch wrote:
很可能不是(至少在非调试版本中)。 RVO(google for that
一个)将启动并删除冗余副本。这就是我所知道的所有现代(2000或更晚)编译器的情况。


我不认为MS VisualStudio 2003附带的编译器

会做RVO,即使是简单的情况。

如果您的编译器无法优化(我怀疑)。如果它不能和你花费很多时间返回你的容器,通过引用传递
returnvalue并用swap而不是return返回:
void tokenize(string const& s ,矢量< string>& result){
Most likely not (at least in a non-debug build). RVO (google for that
one) will kick in and remove the redundant copy. This is the case for
all modern (2000 or later) compilers I know.
I don''t think that the compiler that comes with MS VisualStudio 2003
does RVO, even for simple cases.
It could if your compiler cant optimise (which I doubt). If it can''t
and you spend to much time returning your container, pass the
returnvalue by reference and finish with a swap instead of the return:
void tokenize(string const& s,vector<string>& result){




是的,这很难看,但它确实有效 - 这只取决于你是否更喜欢
更喜欢优化(过早?)或保留更自然的语法。

有时你可以从getCollection重铸函数

" fillCollection"使优化形式看起来更自然。无论如何,

滚动移动构造函数!



Yeah this is ugly but it works - it just comes down to whether you
prefer to optimise (prematurely ?) or retain a more natural syntax.
Sometimes you can recast a function from "getCollection" to
"fillCollection" to make the optimised form look more natural. Anyway,
roll on move constructors !


文章< Wa *********** ***@fe12.lga>,

Sanjay Kumar< no ************** @ yahoo.com>写道:
In article <Wa**************@fe12.lga>,
Sanjay Kumar <no**************@yahoo.com> wrote:
人们,

很长一段时间后我又回到了C ++,我有这个简单的问题:如何做一个问题STL容器
喜欢说矢量或地图(往返于函数)?
Folks,

I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?




与标准算法相同。 ''std :: copy''例如接受

一个容器作为输入参数,另一个容器作为输出参数。



The same way the standard algorithms do. ''std::copy'' for example accepts
a container as an input param, and another container as an output param.


这篇关于如何传递STL容器(比如矢量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆