我想寫一個存儲字符串在數組中的代碼。我試圖用char *來實現,但我無法實現。我搜索網絡,但無法找到答案。我試過下面的代碼,但它沒有compile.I使用字符串流,因爲在某些時候我需要連接一個字符串與一個整數。存儲字符串
stringstream asd;
asd<<"my name is"<<5;
string s = asd.str();
char *s1 = s;
我想寫一個存儲字符串在數組中的代碼。我試圖用char *來實現,但我無法實現。我搜索網絡,但無法找到答案。我試過下面的代碼,但它沒有compile.I使用字符串流,因爲在某些時候我需要連接一個字符串與一個整數。存儲字符串
stringstream asd;
asd<<"my name is"<<5;
string s = asd.str();
char *s1 = s;
>我試圖寫存儲字符串在陣列的代碼。
那麼,首先你需要一串字符串。我不喜歡用裸陣列,所以我用std::vector
:
std::vector<std::string> myStrings;
但是,我明白你必須使用一個數組,所以我們將使用而不是數組:
// I hope 20 is enough, but not too many.
std::string myStrings[20];
int j = 0;
>我用字符串流,因爲......
好了,我們將使用字符串流:
std::stringstream s;
s << "Hello, Agent " << 99;
//myStrings.push_back(s.str()); // How *I* would have done it.
myStrings[j++] = s.str(); // How *you* have to do it.
這就使我們一個字符串,但你希望他們的數組:
for(int i = 3; i < 11; i+=2) {
s.str(""); // clear out old value
s << i << " is a" << (i==9?" very ":"n ") << "odd prime.";
//myStrings.push_back(s.str());
myStrings[j++] = s.str();
}
現在你有一個字符串數組。
完成,測試程序:
#include <sstream>
#include <iostream>
int main() {
// I hope 20 is enough, but not too many.
std::string myStrings[20];
int j = 0;
std::stringstream s;
s << "Hello, Agent " << 99;
//myStrings.push_back(s.str()); // How *I* would have done it.
myStrings[j++] = s.str(); // How *you* have to do it.
for(int i = 3; i < 11; i+=2) {
s.str(""); // clear out old value
s << i << " is a" << (i==9?" very ":"n ") << "odd prime.";
//myStrings.push_back(s.str());
myStrings[j++] = s.str();
}
// Now we have an array of strings, what to do with them?
// Let's print them.
for(j = 0; j < 5; j++) {
std::cout << myStrings[j] << "\n";
}
}
我需要一個動態數組,像用戶輸入第21個字符串,我應該創建一個新的數組,其大小更大。 – berkay 2012-04-07 21:05:33
我可以建議您先讓程序使用固定大小的數組,然後再嘗試添加動態數組。如果(但僅限於)你不知道動態數組是如何獨立工作的,請回過頭再提出另一個問題。 – 2012-04-07 21:11:15
如果你想要一個動態數組,使用一個向量,因爲@Rob正在顯示你。 – 2012-04-07 21:11:17
char *s1 = s;
是非法的。你要麼需要:
const char *s1 = s.c_str();
,如果你不char*
設置,或者你需要分配一個新的char*
和使用strcpy
將內容從字符串複製。
只需更改您的代碼
char const* s1 = s.c_str();
因爲字符指針不能存儲一個String對象,只有一個字符指針,這是c_str()返回。
你確定這會編譯? – 2012-04-07 20:47:19
'c_str'返回一個'const char *'。 – 2012-04-07 20:47:22
@LuchianGrigore從技術上講,指針可以指向對象,不是嗎?儘管在這一點上我可能習慣了C#。 – 2012-04-07 20:49:10
這樣的事情呢?
vector<string> string_array;
stringstream asd;
asd<<"my name is"<<5;
string_array.push_back(asd.str());
我未被允許使用矢量。 – berkay 2012-04-07 20:50:02
你應該把它放在問題中。另外,這是否意味着這是作業?你應該這樣標記它。 – 2012-04-07 21:11:04
我不會直接使用的char *。我會用下面的模板來包裝它。您可以重寫需要做更多操作的操作符(例如,我將數據設爲私有成員,並覆蓋操作符以使數據完整地打印出來)。我做了賦值操作符只是爲了演示如何清理代碼。
#include "MainWindow.h"
#include <stdio.h>
using namespace std;
template<size_t size>
class SaferChar
{
public:
SaferChar & operator=(string const & other)
{
strncpy(data, other.c_str(), size);
return *this;
}
char data[size];
};
int main(int argc, char *argv[])
{
SaferChar<10> safeChar;
std::string String("Testing");
safeChar = String.c_str();
printf("%s\n", safeChar.data);
return 0;
}
不要在C++中使用原始'char *',除非你絕對必須。 – 2012-04-07 20:46:34
數組在哪裏?使用給定的代碼,當前的答案(建議調用'c_str()')將會失敗,如果數組超過's'。 – 2012-04-07 20:48:40
「我試圖寫一個存儲字符串在數組中的代碼」? *爲什麼*? – Johnsyweb 2012-04-07 20:49:46