C++ std::string assign() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::assign() replaces all characters with new content.

It is like operator= with extra overloads. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Pass the new contents.

C++
s.assign("hello");
s.assign(5, '*');
s.assign(other, 1, 3);
CallWhat it does
s.assign(str)Replace contents with str.
s.assign(n, ch)Replace with n copies of ch.
s.assign(cstr)Replace with a C-string.

s = "x" is often enough. Use assign when you need a substring or fill overload.

Library declarations
C++
string& assign(const string& str);
string& assign(size_type n, char c);
string& assign(const char* s);

Parameters

ParameterDescription
sourceNew contents: string, C-string, fill count, iterators, etc.

Return Value

Return ValueDescription
string&Reference to *this.

C++ Examples

Example 1 — Basic assign()

Replace with a new word.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "old";
    s.assign("new");
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The string starts as "old".
  2. assign("new") replaces every character.
  3. Printed text is "new".

Example 2 — Assign Repeated Char

Fill with five dashes.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    s.assign(5, '-');
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. assign(5, '-') builds five dashes.
  2. Any previous text is discarded.
  3. Size becomes 5.

Example 3 — Assign Part of Another

Take a slice from another string.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "xx";
    std::string b = "CodeToFun";
    a.assign(b, 4, 2);
    std::cout << a << '\n';
    return 0;
}

How It Works

  1. From b, index 4 length 2 is "To".
  2. That slice becomes the full contents of a.
  3. Earlier "xx" is gone.

Frequently Asked Questions

It replaces the entire contents of the string.
operator= covers common cases; assign adds fill and substring forms.
It may reuse capacity when the new size fits; otherwise it reallocates.
Yes, for bad positions or allocation failure.
C++23 version that assigns from a range.

Next: assign_range()

Keep going through the string functions. Replace contents with all characters from a range (C++23).

assign_range() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful