Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string::assign vs std::string::operator=

I coded in Borland C++ ages ago, and now I'm trying to understand the "new"(to me) C+11 (I know, we're in 2015, there's a c+14 ... but I'm working on an C++11 project)

Now I have several ways to assign a value to a string.

#include <iostream>
#include <string>
int main ()
{
  std::string test1;
  std::string test2;
  test1 = "Hello World";
  test2.assign("Hello again");

  std::cout << test1 << std::endl << test2;
  return 0;
}

They both work. I learned from http://www.cplusplus.com/reference/string/string/assign/ that there are another ways to use assign . But for simple string assignment, which one is better? I have to fill 100+ structs with 8 std:string each, and I'm looking for the fastest mechanism (I don't care about memory, unless there's a big difference)

like image 524
malarres Avatar asked Dec 10 '15 07:12

malarres


People also ask

What is the difference between string and std::string?

Differences between std::string and String Pavan. std::string is the string class from the standard C++ library. String is some other string class from some other library. It's hard to say from which library, because there are many different libraries that have their own class called String.

Does std::string have operator?

std::string::operator= Assigns a new value to the string, replacing its current contents.

What does std::string () do?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.


2 Answers

Both are equally fast, but = "..." is clearer.

If you really want fast though, use assign and specify the size:

test2.assign("Hello again", sizeof("Hello again") - 1); // don't copy the null terminator!
// or
test2.assign("Hello again", 11);

That way, only one allocation is needed. (You could also .reserve() enough memory beforehand to get the same effect.)

like image 179
emlai Avatar answered Sep 20 '22 19:09

emlai


I tried benchmarking both the ways.

static void string_assign_method(benchmark::State& state) {
  std::string str;
  std::string base="123456789";  
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    str.assign(base, 9);
  }
}
// Register the function as a benchmark
BENCHMARK(string_assign_method);

static void string_assign_operator(benchmark::State& state) {
  std::string str;
  std::string base="123456789";   
  // Code before the loop is not measured
  for (auto _ : state) {
    str = base;
  }
}
BENCHMARK(string_assign_operator);

Here is the graphical comparitive solution. It seems like both the methods are equally faster. The assignment operator has better results.

Use string::assign only if a specific position from the base string has to be assigned.

like image 22
Boanerges Avatar answered Sep 24 '22 19:09

Boanerges