1

I want to construct a std::string object like an array:

std::string str("");
str[0] = 'A';
str[1] = 'b';
str[2] = 'h';
str[3] = 'i';
str[4] = '\0';
std::cout<<str;

But it doesnt print the string. What am i missing?

5 Answers 5

9

Firstly, std::string is not a C-string. You do not need to NULL-terminate it. Secondly, the [] operator is only valid for indices which are < std::string::length(), meaning that at least N elements must be allocated in advance before you can access an element between 0 and N-1.

std::string str(4); // construct a string of size 4
str[0] = 'A';
str[1] = 'b';
str[2] = 'h';
str[3] = 'i';
std::cout << str;

Edit: But also see Johnsyweb's answer. The big advantage of std::string over C-strings is that you don't have to worry about memory allocation. You can use the += operator or push_back member function, and can build the string character-by-character without worrying about how much memory to reserve.

Sign up to request clarification or add additional context in comments.

2 Comments

shouldn't it be std::string(4, ' '); ?
@Abhi: the second argument is optional, if not present it will default to '\0' (instead of the space). It is up to you to determine whether you prefer uninitialized elements to be spaces or NUL characters.
4

Try

std::string (4, ' ');

instead of

std::string("");

basic_string's operator[] returns a reference to the specified character, but since your string is empty, it contains no characters.

2 Comments

Actually I don't like that constructor of std::string because if you can't remember the order and put the character first it will still compile and give you the wrong thing. If you do std::string(' ', 4) it will give you a string of length 32 (ASCII value of space) filled with ASCII 4 characters. Unfortunate problem of char having implicit conversions with size_t.
@CashCow: I agree, it's unseemly that char would both represent a character and behave as an integer...
2

What am i missing?

You are missing the whole point of using a std::string. That approach may work for arrays of char, but not for strings.

Consider std::string::operator += instead.

2 Comments

In all likelihood, this is actually the best answer.
To be honest, I cannot even understand why anyone would want to take that approach with a char array.
1

You allocated the string to be "", that is, exactly 0 bytes long.

You are then trying to write chars outside of bounds of the string - which doesn't work.

2 Comments

I see. Isnt there a way to modify the string declared above then except using insert()?
@Abhi: Look at the documentation for string ( SGI, C++Reference ). There are ( too ) many different member functions that operate on the string, including resize() that will, well, resize the string so you can make it grow.
-1

you should create a space in the memory for your array

using namespace std;

char str[5];

str[0] = 'A';    
str[1] = 'b';    
str[2] = 'h';    
str[3] = 'i';    
str[4] = '\0';

cout << str ;

1 Comment

i want to use std::string. I know i can do that with a char array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.