English 中文(简体)
究竟是怎样才能将扼杀作为理由传入功能?
原标题:which is the Memory efficient way to pass the string as argument to a function.?

任何人都可以告诉我。 是否有任何好处,可以将扼杀作为(扼杀和amp;)而不是指针。

最佳回答

以参考方式通过物体,意味着你以价值重过参引<>strong>,而不是通过/object,这意味着你必须在工作时填写一份副本。

However, passing-by-reference introduces a new set of questions which you must be aware of:

  1. does the function modifies or not the passed object? If it does not, you should put in the const modifier
  2. does the function need to modify the object, but not exposing the modifications outside the function boundaries? In that case what you really want is a copy.
  3. does the function stores somewhere/somehow the reference of the passed object? In that case, you have to understand how object ownership is passed and when it s safe to delete it. You may want to use smart pointers to deal with these issues

The point is: just because passing-by-reference makes function invocation cheaper does not means that you must use it in any case.

问题回答

func(string &> > 经由参考通过。 这意味着它不会被复制,而职能可以修改。

func(string)上乘以数值标示。 这意味着将制作一份复印件,而且这一功能只能修改它自己的当地版面。

未经复制便通过体形,但不得修改使用<条码>。

如果你的职能需要从任何方面提取论据副本,则按价值推移更为可取。

在复杂的类型中,通过提及通常会更为有效,但应当提及<条码>const。 除非你希望能够修改这一职能:

void f(string);        // function gets its own copy - copying may be expensive
void f(string&);       // function can modify the caller s string
void f(string const&); // function can read the caller s string, but not modify it

也可以使用<代码>func(const string & )而不是func(string & ,以确保通过优惠参数不会在职能范围内被错误改动。

When you pass it as string the string first gets copied and the copy is sent to the function. The best way to pass expensive objects such as strings, if you don t want the function to modify them, is by const string&.

<代码>const很重要。 首先,它确保在职能范围内修改扼杀金。 另外,你无法通过字面,不使用“密码”

就业绩而言,通过参考通常优于通过价值(不包括内在数据类型)。 因此,func(string &>优于func(string)。 条件是,在职能结束时,可修改已通过的<条码>。 如果您无意修改<条码>,。 然后使用<代码>func(const string &)。

On the other note, I remember reading somewhere that in STL, the strings are optimized. Means if you pass string by value, it may not necessarily create a new string on heap. So passing by value may not be that much expensive as you expect. e.g.

string s1 = "hello";  // new buffer allocated on heap and copied "hello"
string s2 = s1;  // s2 and s1 both  may  refer to same heap 
...
s1 += " world"; // s2 continue referring to old heap ...
                // new heap created to accomodate "hello world" referred by s1

Yes. Passing a reference is always efficient.
A string may be stored by value and the copying may take as long as O(n) (i.e., copying takes time proportional to the length of the string). It will also cause heap memory to be allocated, which is usually far more expensive than the copy.
You may also consider passing by reference-to-const i.e. func(const string &) to avoid any unintenional changes.

你们也可以利用移动学分,努力利用高价值参考优化。

void func(string &&s)
{
    myS = std::move(s);
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...