2011-05-31 64 views
0

我有一個字符串對象的引用,我如何從中獲取數據。這裏是我的示例:獲取參考文獻的內容

string key = "key1"; 
gpointer somepointer; 

GHashTable* myTable; 
g_hash_table_insert(myTable,&key1,somepointer);   

GList *keysList = g_hash_table_get_keys(myTable);// here i got keys previously set 
keysList = g_list_first(keysList); 
string recentKey = (keysList->data); 

數據引用字符串的引用。我怎樣才能從參考

+0

如果'data'是'串'(到有效位置)那麼你的語句'string recentKey =(keysList-> data);'是適當的。您是否嘗試過打印或檢查它? – iammilind 2011-05-31 04:57:45

回答

1

如果keysList->datagpointervoid*),我想下面是需要一些鑄像 :

string recentKey = *(string*)keysList->data; 

希望這有助於

+0

許多很多謝謝.... – boom 2011-06-01 04:54:05

+0

很高興它幫助:-) – 2011-06-01 13:04:01

1

如果數據是引用一個字符串,然後檢索數據,

keysList->數據返回的字符串。

#include<iostream> 
#include <cstring> 
#include <string> 

using namespace std; 

int main() 
{ 
    string MyString("ABCD"); 
    string &MyString2 = MyString; 
    char * cstr; 

    cout<<"\n"<<MyString; 
    cout<<"\n"<<MyString2; 

    cstr = new char [MyString.size()+1]; 
    strcpy (cstr, MyString.c_str()); 

    cout<<"\n"<<cstr; 
    delete[] cstr; 

    return 0; 
} 
0

你是什麼意思?「我如何從參考中檢索數據?」你嘗試過嗎?

std::string中的唯一數據是字符串長度和有效負載char*數組。
keysList->data.length()將訪問該長度,而keysList->data.c_str()將訪問char*陣列。

你的最後一句話是參考獲取數據:

string recentKey = (keysList->data); 

此語句創建一個字符串的完整副本。由於它是一個副本,所以對recentKey的任何修改都不會顯示在keysList->data中。這是一件好事還是壞事取決於你的意圖。

+0

但數據是無效的*我試圖顯示的值,它顯示內存的參考,而不是值.... – boom 2011-05-31 09:07:03