1
string a;
string seq="AACCGGTT";

for (int i=0;i<seq.length();i++)
{
    if (seq[i]=='T')
    {
        a[i]='U';
        cout<<"i have turned: "<<seq[i]<<" into : "<<a[i]<<endl;
    }
    else
    {
        a[i]=seq[i];
        cout<<"i have turned: "<<seq[i]<<" into : "<<a[i]<<endl;
    }
}

cout<<"here: "<<a<<endl;

**

  • when i run this part of a big code it prints

i have turned: A into : A

i have turned: A into : A

i have turned: C into : C

i have turned: G into : G

i have turned: G into : G

i have turned: T into : U

i have turned: T into : U

here:

and it doesn't return the value of a

**

Community
  • 1
  • 1
  • 6
    `a` is empty. You can’t just index it outside its bounds. – Sami Kuhmonen Dec 08 '18 at 13:07
  • 1
    Instead of using `[ ]`, replace with `a.at(i)='U';` and `a.at(i)=seq[i];`. Then you will see the error you're making by [having an exception thrown](https://en.cppreference.com/w/cpp/string/basic_string/at). – PaulMcKenzie Dec 08 '18 at 14:41

4 Answers4

3

The index operator [] does not append anything to a it can only give you access to existing characters in a. Since there are no characters in a, each of you usages of [] on a is undefined behavior.

To actually append something to a, you can either use a.append('U'); or a += 'U';.

An alternative would be to initialize a as a copy of seq instead of an empty string. Then you can safely use the index operator on it as you are trying right now.

You may also want to consider the replace function as described here.

sebrockm
  • 5,733
  • 2
  • 16
  • 39
1

In your code a is created by the following default constructor. For instance, table 63 in 21.4.2 of n3337(draft) says

explicit basic_string(const Allocator& a = Allocator());

data(): a non-null pointer that is copyable and can have 0 added to it
size(): 0
capacity(): an unspecified value

Therefore a[i] in your code can show something undefined behavior.

I think that the most simple way to replace a specific character is using std::replace as follows.

DEMO

#include <iostream>
#include <string>
#include <algorithm>

int main()
{    
    std::string seq="AACCGGTT";
    std::replace(seq.begin(), seq.end(), 'T', 'U');
    
    std::cout << seq << std::endl; // "AACCGGUU"
    
    return 0;
}
Community
  • 1
  • 1
Hiroki
  • 2,780
  • 3
  • 12
  • 26
0

Change a+=seq[i] instead a[i]=seq[i](ofcourse a+='U' to) problem is a[i] cant be a part of a cause a is empty when you set a[i] in your loop a[i] represents diffirent variable for compiler.

  • More generally, `a[i]` isn't a part of `a` if `i` is greater than or equal to `a.length()`. You're right that when `a` is empty no index is valid; `a.length()` is 0, so there is no index value that's greater than or equal to `a.length()`. – Pete Becker Dec 08 '18 at 13:36
0

Because you haven't set the size of your string a, it has only reserved as much space as your implementation's small string optimization provides. (But note that even so, indexing into the string before initializing it is undefined behavior.)

You can fix this by setting the size of a like so:

string seq="AACCGGTT";
string a;
a.resize(seq.size();
s3cur3
  • 2,749
  • 2
  • 27
  • 42
  • Well, apparently his program is not crashing. Otherwise, I'm sure he would have noticed. Crashing is only one of the infinitely many behaviors that can happen in a case of undefined behavior. – sebrockm Dec 08 '18 at 13:37
  • Ah, right you are... I misread it and thought the last cout wasn’t happening at all... in fact, it’s just printing the variable empty, because it’s size is zero. – s3cur3 Dec 08 '18 at 13:40