C++ string && char * && int之间的关系及转换

OO~ posted @ 2013年4月14日 20:04 in C/C++ && 算法 , 3286 阅读

    最近一直在忙别的事情,没有多少时间来写东西,今天算是抽出一点空闲,聒噪一下最近遇到的问题。

    开始之前,说下自己的感触。抽象一下计算机or程序,其实你会感觉很简单,输入,处理,输出。在输入这一块里,基本都是字符类型,所以搞清楚如何处理这种类型的数据很重要。甚至从我一直在学习的python来看,这个道理也非常适用!

    不说废话了!string其实是作为C++的一个类存在,我们在声明string str的时候,其实就是声明了一个类的对象。这个类有很多内建函数,例如+(链接字符串的操作),length()等等。而char是C/C++的一个基本类型,一个关键字(string不是C++的关键字),char *声明的是一个指针。

    因为在使用char *的时候,指针空间大小的分配需要注意,所以string的出现方便了这一系列的问题。至于string如何在char *以及int之间转换,其实也很简单,不过在谈论如何转换前,我们再说下stringstream(流的输入输出操作),它是C++新增的<sstream>库中定义的其中一个类,还有两个类分别是istringstream(流的输入操作), ostringstream(流的输出操作)。需要注意一点的是,<sstream>使用string对象来代替字符数组,这样可以避免缓冲区溢出的危险,而且,传入参数和目标对象的类型被自动推倒出来,即使使用了不正确的格式化符号也没有危险(但是如果你想多次使用同一个stringstream对象,记得每次使用前使用clear()方法——stringstream对象的构造和析构都非常耗用CPU时间,所以这样重复利用比较好)。

    int 转 string:

stringstream ss;
string str;
int n;
ss << n;
ss << str;

    string转int:

string str;
int n = atoi(str.c_str());

    string转char *:

string str;
char *a = str.c_str();

     char *转string:

string str;
char *a = "abc";
str(a);//初始化即可

    atoi()意思是array to int,而c_str()返回的是pointer,所以这里算是比较好理解把。

PS:推荐一个比较好的C++函数以及库的说明查找网站www.cplusplus.com.

Avatar_small
cleaning services in 说:
2019年9月15日 20:11

We've got an extensive variety of experience throughout construction cleaning contour post development cleaning by simply understanding your current exact features. You will notice that the manpower is professional and are generally specialized inside construction cleaning in spite of size, spot or variety, etc. Fortunately they are specialized inside pre-build cleanup service to be sure standard care and safe and sound conditions prior to actual construction comes about.

Avatar_small
freezebusinessrates 说:
2019年11月14日 02:03

If you desire a laser printer to make labels, then you could be bound to find confused quite simply due to its choices which were available for sale. There would be a different quantity of printers which were used towards print producing labels, you desire some tips to determine the best Recording label printer as reported by your obligation.

Avatar_small
rapidity news 说:
2020年3月21日 17:27

You might want to become a good Google Current information publisher. Usually do not worry, you will not be a good 'Times Newspaper' or simply a 'Wall Path Journal'. You could set up your own private news site very easily. Look for news solutions on Msn News and you will find that there are plenty of ordinary websites on there.

Avatar_small
eastern iowa news 说:
2020年3月21日 17:28

Browsing the documents online together with watching 24-hour current information sites is becoming more and more popular. The reason is , it is certainly cheaper also, you get even more news. You will find what is happening worldwide, as it all happens.

Avatar_small
bio hazard technolog 说:
2020年3月21日 17:28

Technology during the long-run is certainly irrelevant. That could be what complaintant of mine told me when That i made a good presentation that will him a few new system. I were talking with regards to the product's options and features and mentioned "state-of-the-art technology" or simply something to it effect, as one of these.

Avatar_small
thai citrus technolo 说:
2020年3月21日 17:29

Lots of individuals mistakenly believe it is technology which unfortunately drives originality. Yet within the definitions higher than, that is certainly clearly not the case. It is certainly opportunity which unfortunately defines originality and systems which facilitates innovation. See the common "Build the best mousetrap" example of this taught in the majority of business classes.

Avatar_small
handi sports 说:
2020年3月21日 17:29

During the sports sector, peak results in sports is without question a much desirable state by just players together with coaches of the levels. Your house athletes happen to be school area soccer competitors or Olympians striving thus to their Gold medals, best performance during sports has got always pulled in athletes together with coaches identical.

Avatar_small
monthly maid service 说:
2020年4月28日 20:11

You should also enquire with the previous individuals about your own home maid will take her possess vacuums, detergents, mops, cloths, pails and even polishes or you ought to provide them all. Make sure to make a extraordinary training and advice n the right way to clean that antique house furniture, oriental nice carpets, and receptive show articles; also show you them how to deal with with these foods.

Avatar_small
construction paintin 说:
2020年4月28日 20:11

Face painting is produced due to their effort and even talent in portrait artists. Without them all, this type art wouldn't exist. In actual fact, portrait artists have a whole lot of contribution with the art trade. Agree and not, efficient the individuals that harvest the information of ones own skills. For that reason, we like the appearance in portraits particularly if they can be imitated belonging to the works in famous face painters.

Avatar_small
cleaning company dub 说:
2021年6月07日 14:02

And additionally, this may include kitchen court surfaces and underneath the dining rooms. Moreover, weekly or possibly daily cleaning is completely while using amount about dirt or possibly grime that features accumulated in your. However, you will also opt just for curtain cleaning to earn your family home germ-free.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter