我想写一个存储字符串在数组中的代码。我试图用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