codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

79 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/<string> Reference
⚡C++ Programming

<string> Reference

Updated 2026-04-20
2 min read

Introduction

The <string> header in C++ is a fundamental part of the Standard Library, providing facilities for working with strings. This tutorial will cover the key features and functionalities of the std::string class, including its constructors, methods, and best practices for string manipulation.

Overview of std::string

std::string is a sequence container that represents arrays of characters and is part of the Standard Template Library (STL). It provides a high-level interface for manipulating strings, offering both performance and ease of use. The std::string class is defined in the <string> header.

Constructors

Default Constructor

#include <iostream>
#include <string>

int main() {
    std::string str; // Creates an empty string
    return 0;
}

Copy Constructor

std::string str1 = "Hello";
std::string str2(str1); // Copies the contents of str1 to str2

Move Constructor

std::string createString() {
    return std::string("Move this string");
}

int main() {
    std::string str3 = createString(); // Move semantics are used here
    return 0;
}

String Literal Constructor

std::string str4("Hello, World!"); // Initializes with a C-style string literal

Substring Constructor

std::string str5(str1, 7, 5); // Creates "World" from str1 starting at index 7 and length 5

Methods

Accessing Elements

  • operator[]: Provides access to characters by index.
  • at(): Similar to operator[], but throws an exception if the index is out of range.
std::string str6 = "Hello";
char c1 = str6[0]; // 'H'
char c2 = str6.at(0); // 'H'

Modifying Strings

  • append(): Appends a string to the end.
  • push_back(): Adds a single character at the end.
str6.append(" World!"); // "Hello World!"
str6.push_back('!'); // "Hello World!!"

Removing Characters

  • pop_back(): Removes the last character.
  • clear(): Erases all characters in the string.
str6.pop_back(); // "Hello World!"
str6.clear(); // ""

String Comparison

  • compare(): Compares two strings lexicographically.
std::string str7 = "Apple";
int result = str7.compare("Banana"); // Negative value, since 'A' < 'B'

Searching and Replacing

  • find(): Searches for a substring.
  • replace(): Replaces a portion of the string.
std::string str8 = "Hello, World!";
size_t pos = str8.find("World"); // 7
str8.replace(pos, 5, "Universe"); // "Hello, Universe!"

Substrings

  • substr(): Extracts a substring.
std::string subStr = str8.substr(0, 5); // "Hello"

Best Practices

  1. Use std::string Over C-style Strings: std::string provides automatic memory management and is safer to use.
  2. Avoid Unnecessary Copies: Use move semantics where possible to avoid copying large strings.
  3. Exception Safety: Prefer methods like at() over operator[] when bounds checking is necessary.
  4. Use std::string_view for Read-Only Access: When you need a lightweight, non-owning view of a string.

Real-world Example

#include <iostream>
#include <string>

void processString(std::string str) {
    // Process the string
    std::cout << "Processing: " << str << std::endl;
}

int main() {
    std::string input = "Hello, World!";
    processString(input); // Pass by value to allow modifications

    const std::string_view view = input; // Use string_view for read-only access
    std::cout << "Viewing: " << view << std::endl;

    return 0;
}

In this example, processString takes a copy of the string, allowing it to modify the input without affecting the original. The std::string_view is used for read-only access, which is more efficient than copying the entire string.

Conclusion

The <string> header in C++ provides a powerful and flexible way to handle strings. By understanding its constructors, methods, and best practices, you can write efficient and safe C++ code that manipulates strings effectively.


Previous<cmath> ReferenceNext <cstring> Reference

Recommended Gear

<cmath> Reference<cstring> Reference