标题:请教string的赋值问题
取消只看楼主
kspliusa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:98
专家分:178
注 册:2009-9-27
结帖率:80%
已结贴  问题点数:30 回复次数:2 
请教string的赋值问题
// 编译环境  DEV C++
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string str_text_one;
    char source[] = "Hello";
   
    int i=0;
    while (source[i]) {
        str_text_one[i] = source[i]; // 这样赋值为啥不行呢
        i++;               
    }
    cout << "the length :" << str_text_one.length () << endl;
    cout << "str_text_one :" << str_text_one << endl;
     
    cout << "str_text_one :";
    for (i=0; i<strlen(source); i++) // 这样输出str_text_one中有值
        cout << str_text_one[i];
    cout << endl;
   
    str_text_one = source; // 这样赋值可以 上面为啥不行呢?
    int str_text_one_len = str_text_one.size ();
   
    cout << "str_text_one_len = " << str_text_one_len <<endl;
    cout << "str_text_one is :" << str_text_one << endl;
   
    system ("PAUSE");
    return 0;
}
问题如注释。。。
搜索更多相关主题的帖子: string 赋值 
2010-04-11 22:24
kspliusa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:98
专家分:178
注 册:2009-9-27
得分:0 
回复 2楼 yyblackyy
可以讲讲为什么吗?结果不是目的,目的是搞清楚原理啊。。。请指教。。。。
2010-04-12 12:45
kspliusa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:98
专家分:178
注 册:2009-9-27
得分:0 
回复 4楼 yyblackyy
可是向后面这样输出为什么还有值啊?for (i=0; i<strlen(source); i++) // 这样输出str_text_one中有值
                                       cout << str_text_one[i];
??
2010-04-13 22:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-302613-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.097506 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved