線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2154
推到 Plurk!
推到 Facebook!

如何將string 直接assign 給AnsiString??

尚未結案
plihui
初階會員


發表:88
回覆:96
積分:41
註冊:2003-07-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-19 20:26:59 IP:218.168.xxx.xxx 未訂閱
string i="abcd"
AnsiString Data_s=i;
出現錯誤 如何將string 直接assign 給AnsiString??
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-19 20:34:41 IP:211.22.xxx.xxx 未訂閱
引言:
string i="abcd"
AnsiString Data_s=i;
注意大小寫及結束符號! < class="code"> String i="abcd"; AnsiString Data_s=i; ShowMessage(Data_s); ---------------------------------------------- We will either find a way, or make one. -Hannibal -。
plihui
初階會員


發表:88
回覆:96
積分:41
註冊:2003-07-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-19 20:58:22 IP:218.168.xxx.xxx 未訂閱
很感激大大的指倒 不過...我是指  #include  中的string耶?! 那這種狀況有解嗎?
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-09-19 21:19:04 IP:211.22.xxx.xxx 未訂閱
很感激大大的指倒
不過...我是指 
#include 
中的string耶?!
那這種狀況有解嗎?
這個String.h是標準C中的字串處理函示的標頭檔,不能當作型別來宣告,而String i 的這個String是類別,可以當作型別宣告,不要搞混了。 ----------------------------------------------
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-09-20 02:44:00 IP:211.76.xxx.xxx 未訂閱
你指的是help中的 A string is basicallya sequence of characters that can be indexed. In fact, although a string is not declared as a subclass of vector, almost all the vector operators discussed in the vector data abstraction (and subsequent topics) can be applied to string values. Indeed, a string qualifies as a sequence container type. However, a string is also a much more abstract quantity. In addition to simple vector operators, the string datatype provides a number of useful and powerful high level operations.    In the Standard C++ Library, a string is actually a template class, named basic_string. The template argument represents the type of character that will be held by the string container. By defining strings in this fashion, the Standard C++ Library not only provides facilities for manipulating sequences of normal 8-bit ASCII characters, but also for manipulating other types of character-like sequences, such as 16-bit wide characters. The datatypes string and wstring (for wide string) are simply typedefs of basic_string, defined as follows:    typedef basic_string string; typedef basic_string wstring; Note: In the remainder of this section, we refer to the string datatype, but all the operations we introduce are equally applicable to wide strings. As we have already noted, a string is similar in many ways to a vector of characters. Like the vector datatype, a string is associated with two sizes. The first represents the number of characters currently being stored in the string; the second is the capacity, the maximum number of characters that can potentially be stored in a string without reallocation of a new internal buffer. As in the vector datatype, the capacity of a string is a dynamic quantity. When string operations cause the number of characters being stored in a string value to exceed the capacity of the string, a new internal buffer is allocated and initialized with the string values, and the capacity of the string is increased. All this occurs behind the scenes, requiring no interaction with the programmer. Include files Programs that use strings must include the string header file: # include 這個string嗎? 是的話應該可以用strcpy去拷貝吧? 另外,忘了從哪聽到的,String是用類似define的方式定義的,也就是說 String本身就是AnsiString 不知道對不對@@"
zcecil
初階會員


發表:0
回覆:28
積分:45
註冊:2003-06-27

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-09-20 10:02:41 IP:220.130.xxx.xxx 未訂閱
用 string 的 c_str() method 可以拿到 const char*, 指向字串的內容. 再用 ansistring的constructor吃就可以了. 如下: string i="abcd" AnsiString Data_s=AnsiString(i.c_str());
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-09-21 17:39:53 IP:211.76.xxx.xxx 未訂閱
剛剛試了一下,string會有錯,結果跑去翻string(在.\Borland\CBuilder6\Include\Vcl下)    下面是string的內容...
/*
 * Copyright (c) 1997-1999
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This material is provided "as is", with absolutely no warranty expressed
 * or implied. Any use is at your own risk.
 *
 * Permission to use or copy this software for any purpose is hereby granted 
 * without fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 *
 */    #ifndef _STLP_STRING
# define _STLP_STRING    # ifndef _STLP_OUTERMOST_HEADER_IDbasic_string
#  define _STLP_OUTERMOST_HEADER_ID 0x68
#  include 
# endif    #if defined (_STLP_USE_NATIVE_STRING)    // as part of compiled runtime library depends on it.    # if defined (_STLP_MSVC)
#  include 
#  include 
# endif    #  include _STLP_NATIVE_HEADER(string)    # endif /* _STLP_USE_NATIVE_STRING */    # if !defined (_STLP_USE_NATIVE_STRING) || defined (_STLP_USE_OWN_NAMESPACE)
# include 
# else
# include 
# endif /*_STLP_USE_NATIVE_STRING */    // cleanup    # if (_STLP_OUTERMOST_HEADER_ID == 0x68)
#  include 
#  undef _STLP_OUTERMOST_HEADER_ID
# endif    #endif /* _STLP_STRING */    // Local Variables:
// mode:C  
// End:
裡面似乎沒有定義"string"這個資料型態或類別 你確定你的string i="abcd"的string是用#include <string>出來的? zcecil大大說的.c_str是在AnsiString或String中才有的 因為我找不到"string"這個資料型態或類別,所以也不知道"string"下有沒有c_str可以用
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-09-21 17:50:43 IP:211.76.xxx.xxx 未訂閱
不小心按到了@@" 如果你要把const string設給AnsiString的話可以直接用下面這個方式
        char *i = "abcd";
        String s = i;
如果你要把AnsiString設給char string的話可以直接用下面這個方式
#include 
...
...        
        String s="ABCD";
        char *i;
        strcpy(i,s.c_str());
PS..剛剛查了一下help,只有AnsiString中有c_str()可以用 當然,如果有其他東西繼承了AnsiString的話我就不清楚了..
zcecil
初階會員


發表:0
回覆:28
積分:45
註冊:2003-06-27

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-09-21 18:56:59 IP:220.130.xxx.xxx 未訂閱
#include < string > 的定義在 Include\stl 下的 _string.h 裡(請注意看stlport\string, 它其實是有 include stl\_string.h 的) 可以正常編過的程式如下: #include < string > using namespace std; int main() { string a="123"; AnsiString b = a.c_str(); } 請確定你的程式裡沒有漏了 using namespace std; 順帶一提 String 是 delphi 的type, 在 BCB 裡對應的是 AnsiString. string 和 String 是不同的兩回事. String(AnsiString) 是BCB附的class, string則是 C 標準程式庫裡的東西. 發表人 - zcecil 於 2005/09/21 19:05:49 發表人 - zcecil 於 2005/09/21 19:07:09
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-09-21 21:37:15 IP:211.76.xxx.xxx 未訂閱
原來如此,感謝zcecil大大的指正,又多學了一個,ㄏㄏ    using namespace std也是因為string則是 C 標準程式庫裡的東西這個的關係吧? 了改嚕,感恩.... 不自覺的將問題複雜化@@
系統時間:2024-06-24 21:25:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!