English 中文(简体)
C++ 修缮果
原标题:C++ change char in string

如果想在地狱中改变一个独有的果园:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string mystring = "Hello";
  mystring[0] =  T ;
  cout << mystring;
  return 0;
}

如果想在使用不同功能进行扼杀时改变单一char:

#include <iostream>
#include <string>
using namespace std;

void changeStr(string* mystring);

int main() {
  string mystring = "Hello";
    changeStr(&mystring);
  cout << mystring;
  return 0;
}

void changeStr(string* mystring)
{
    mystring[0] =  T ;
}

为什么会这样做? 整条插座改为“T”。 预示着方案拟定工作,在要点/地址方面仍然存在一些问题。 我知道,一系列果园(果园)是其第一个指数的点子。 这是否适用于扼杀 as? 如何加以确定?

问题回答

对于开端人来说,通过标的点子将“<>/条码>的物体通过该物体的点子传递该功能是没有意义的。 你可以参考。 因此,这一职能可能看起来像

void changeStr(string &mystring)
{
    mystring[0] =  T ;
}

相应地,这种职能被称作“类似”

changeStr(mystring);

至于你的问题,首先,你需要参照点,而只是在该点之后才适用分稿操作者。

void changeStr(string* mystring)
{
    ( *mystring )[0] =  T ;
}

Alternatively you could write

void changeStr(string* mystring)
{
    mystring->operator []( 0 ) =  T ;
}

Pay attention to that the postfix subscript operator has a higher precedence than the dereferencing operator.

关于你在职能范围内的示范方案中的陈述

mystring[0] =  T ;

然后,在使用转让经营人进行铺设时,将其特性指定为<代码>T。

basic_string& operator=(charT c);

不是只分配脚石的头号。

该说明之后,该说明将等于T>

在<代码>main中,书写方式相同。

mystring =  T ;

。 因此,在你的职责范围内,你甚至可以写字。

mystring[0][0] =  T ;

而不是

( *mystring )[0] =  T ;

尽管正如我早些时候指出的那样,最好通过提及来通过说明。

you don t need to pass the pointer. Simply pass it by reference(reference will update the value) if you pass it by pointer then return the string instead of void.. better option .. pass it by reference

 #include <iostream>
 #include<string>
 using namespace std;
 void changeStr(string &mystring);

 int main() {
 string mystring = "Hello";
  changeStr(mystring);
 cout << mystring;
 return 0;
}

void changeStr(string &mystring)
{
  mystring[0] =  T ;
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签