-4

Suppose I have this code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string disectedString[5];

    disectedString[0] = "011001";

    string temp = disectedString[0];
    string print = temp[0];

    return 0;
}

So I'm selecting an element out of my array of strings, and then assigning it to a temp variable. From there, I want to select the first element out of the temp variable,(the first character). How would I go about doing this?

demogorgon
  • 474
  • 4
  • 20
NULLPTR
  • 37
  • 4

2 Answers2

2

Your intuition is mostly valid: You use the square brackets operator, [], to access the element at an indexed position within a collection or sequence. Thus

  • disectedString[0] means "the first element of disectedString";
  • temp[0] means "the first element of temp";

What you've gotten mixed up are the types, as commenters and @demogorgon.net's answer have explained.

Now, with modern C++ you can "play dumb" and not declare what you know the types to be:

std::string disectedString[5];
disectedString[0] = "011001";
auto temp = disectedString[0];
auto print = temp[0];

Note the use of auto instead of a specific type name. This will work as you would like it to. You can then use use print, and do, for example:

std::cout << print;

and this will output 0.


By the way, I believe you should reconsider your choice of names:

  • Intuitively, print should refer to a function, or a method, which prints things; I'd suggest first_character or char_to_print or just c if you want to be brief.
  • temp is no more a temporary variable than, say, print.
  • It's better to avoid variable names which contain the type name, although we sometimes sort of have to resort to that. Specifically you using the word 'string' in variable names; probably not a good idea.
  • Your disectedString variable is not a string, it's an array of strings, which is confusing.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

A string behaves in many ways like an array of char's (*). You need to set print to char type instead of string since you are trying to get a specific element from the string. So your print should look like this:

char print = temp[0];

(*) but it's really more complicated than that.

Here is a code example that prints the output.

demogorgon
  • 474
  • 4
  • 20
  • 1
    I wouldn't really say `std::string`s are essentially arrays of `char`s. They behave like them to some extent. – einpoklum Jul 13 '18 at 14:58
  • @einpoklum yes that's correct. I only say essentially because on the most basic level, this is easy to comprehend for someone learning c++. but you are right :) – demogorgon Jul 13 '18 at 14:59
  • Also, why do you insist OP used `temp.at()` instead of `temp[0]`? The latter is [perfectly valid code](https://en.cppreference.com/w/cpp/string/basic_string/operator_at). – einpoklum Jul 13 '18 at 15:03
  • ughh you are right, idk why to be honest, especially after I just said its like an array of `chars`. editing it now. @einpoklum – demogorgon Jul 13 '18 at 15:07