At in C++
Introduction
The above at() operation provides an integral procedure that belongs to multiple receptacle forms in C++, particularly std::vector, std::deque, std::map, std::unordered_map, among furthermore. It permits people to gain access to the component at an identified location in the container while also performing bounds checking to guarantee people refrain from accessing elements that are beyond the range.
Syntax
Exactly at() function in std::string provides an additional value that it returns and variable syntax:
char& at(size_t pos);
const char& at(size_t pos) const;
That at() procedure requires one argument, pos, that indicates the exact spot on the character in question throughout the string that needs to be examined. That provides an identity to the character that is at the specified point, permitting you to modify this symbol if the argument was non-const.
Whenever the power source at() function executes against a const std::string object (together with the const qualifier), it yields a const char&, signifying that the contents of the resulting connection is unable to be edited.
Example 1
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
char& firstChar = str.at(0);
const char& fifthChar = str.at(4);
std::cout << "Before modification: " << str << std::endl;
firstChar = 'h';
std::cout << "After modification: " << str << std::endl;
return 0;
}
Output
![At in C++](https://static.tutorialandexample.com/cpp/at-in-cpp1.png)
Explanation
- We declare a std::string variable str with the value "Hello, world!".
- Using str.at(0), we access the first character of str and assign it to firstChar, allowing us to modify it.
- Using str.at(4), we access the fifth character of str and assign it to fifthChar, but since it's const, we can't modify it.
- We output the original value of str.
- We modify the character referenced by firstChar to 'h'.
- Attempting to modify fifthChar results in a compilation error due to its const qualifier.
- We output the modified value of str.
- The software outputs:
- Before modification: Hello, world!
- After modification: hello, world!
This example demonstrates accessing and modifying individual characters of a string using at() in std::string, with an emphasis on const and non-const references.
Example 2
#include <iostream>
#include <string>
int main() {
std::string word = "OpenAI";
for (size_t i = 0; i < word.length() / 2; ++i) {
char temp = word.at(i);
word.at(i) = word.at(word.length() - 1 - i);
word.at(word.length() - 1 - i) = temp;
}
std::cout << "Reversed word: " << word << std::endl;
return 0;
}
Output
![At in C++](https://static.tutorialandexample.com/cpp/at-in-cpp2.png)
Explanation
- We have a string word with the word "OpenAI" in it.
- We iterate from the beginning of the string to the middle character within the for loop.
- We use word.at(i) to get the character at index i for each iteration.
- The character is saved in the temporary variable temp.
- We assign the character at location i to word.length() - 1 - i (which corresponds to the equivalent character from the end of the string).
- Finally, we swap the characters by assigning the character in the temporary variable temp to the place word.length() - 1 - i.
- When the loop is finished, we output the inverted word.
This example shows how to access and edit specific characters in a string using the at() function. It flips the characters in the string "OpenAI" to "IAnePO" in this case.
Example 3
#include <iostream>
#include <string>
int main() {
std::string sentence = "The sun is shining.";
char targetChar = 'i';
int count = 0;
for (size_t i = 0; i < sentence.length(); ++i) {
if (sentence.at(i) == targetChar) {
++count;
}
}
std::cout << "The character '" << targetChar << "' appears " << count << " times." << std::endl;
return 0;
}
Output
![At in C++](https://static.tutorialandexample.com/cpp/at-in-cpp3.png)
Explanation
- We have a string sentence that includes the phrase "The sun is shining."
- We specify a target character targetChar for which we wish to count the number of occurrences.
- We set the counter variable count to 0.
- We iterate over each character in the sentence string using a for loop.
- We utilise the at() method within the loop to get the character at each place.
- If the character matches the targetChar, the count variable is increased.
- We output the number of times the targetChar appears in the string at the end of the loop.
The following instance demonstrates how to retrieve and evaluate particular letters in a string using the at() function. subsequently calculates the total amount of instances an individual character is found in the wording "The sun is shining." The numeral 'i' appeared on three occasions in this particular instance.
Applications
In summary, the C++ 'at()' function has the following applications:
- Using the index or key to access elements in containers.
- Bounds checking to ensure programme safety and restrict access to invalid places.
- Directly modifying items in non-const containers.
- Using'std::string' to manipulate individual characters.
- Handling exceptions and problems while accessing items.
- Creating bespoke container classes with comparable features.
Remember to use the 'at()' method with caution, to handle exceptions, and to check for errors.