How to initialize an std string with a length

0 votes

How can I correctly initialise a string if its length is specified at build time?

#include <string>
int length = 3;
string word[length]; //invalid syntax, but doing `string word = "   "` will work
word[0] = 'a'; 
word[1] = 'b';
word[2] = 'c';

...in order for me to accomplish anything like this?

I'm doing this because I have a loop that copies characters from specific parts of another string into a new string.


Jul 27, 2022 in C++ by Nicholas
• 7,760 points
961 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
411 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,445 views
0 votes
0 answers

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE

Aug 5, 2022 in C++ by krishna
• 2,820 points
678 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
895 views
0 votes
0 answers

How to convert a char array to a string?

Converting a C++ string to a char ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
356 views
0 votes
0 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
614 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,670 points
325 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,012 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
493 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
357 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP