C++ 存储字符串
我正在尝试编写一个将字符串存储在数组中的代码。我试着用char来做,但我没能做到。我在网上搜索,但找不到答案。我尝试了下面的代码,但没有编译。我使用字符串流,因为在某个时候我需要将字符串与整数连接起来C++ 存储字符串,c++,pointers,C++,Pointers,我正在尝试编写一个将字符串存储在数组中的代码。我试着用char来做,但我没能做到。我在网上搜索,但找不到答案。我尝试了下面的代码,但没有编译。我使用字符串流,因为在某个时候我需要将字符串与整数连接起来 stringstream asd; asd<<"my name is"<<5; string s = asd.str(); char *s1 = s; stringstreamasd; 自闭症 这是违法的。您需要: const char *s1 = s.c_str();
stringstream asd;
asd<<"my name is"<<5;
string s = asd.str();
char *s1 = s;
stringstreamasd;
自闭症
这是违法的。您需要:
const char *s1 = s.c_str();
如果未设置为char*
,或者需要分配一个新的char*
,并使用strcpy
从字符串中复制内容。只需将代码更改为
char const* s1 = s.c_str();
因为指向char的指针不能存储string对象,所以只能存储指向char的指针,这就是c_str()返回的内容。类似的东西怎么样
vector<string> string_array;
stringstream asd;
asd<<"my name is"<<5;
string_array.push_back(asd.str());
向量字符串_数组;
stringstream asd;
自闭症
>我正在尝试编写一个将字符串存储在数组中的代码
好吧,首先你需要一根弦。我不喜欢使用裸数组,所以我使用std::vector
:
std::vector<std::string> myStrings;
>我使用字符串流是因为
好的,我们将使用stringstream:
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.
std::strings;
我不会直接使用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;
}
#包括“MainWindow.h”
#包括
使用名称空间std;
样板
类安全管理员
{
公众:
SaferChar和运算符=(字符串常量和其他)
{
strncpy(数据,其他.c_str(),大小);
归还*这个;
}
字符数据[大小];
};
int main(int argc,char*argv[])
{
safercharsafechar;
标准::字符串(“测试”);
safeChar=String.c_str();
printf(“%s\n”,safeChar.data);
返回0;
}
<代码>不要在C++中使用原始<代码> char */COD>除非你绝对必须。数组在哪里?对于给定的代码,当前的答案(建议调用c_str()
)将在数组超过s
“我正在尝试编写一个在数组中存储字符串的代码”时失败?为什么?@Johnsyweb如果调用了correspont数组元素,我将使用该字符串,我将打印它。你确定这会编译吗?c_str
返回一个const char*
@LuchianGrigore从技术上讲,指针可以指向对象,不是吗?虽然这是可能的,但在这一点上我太习惯了。@OliCharlesworth,sry!:)@一个指针也可以指向0x000000。但这没用。我不允许使用向量。你应该把它放在问题中。还有,这是否意味着这是家庭作业?你应该这样标记它。我需要一个动态数组,就像用户输入第21个字符串一样,我应该用相同的名称创建一个新数组,其大小更大。我建议你先让你的程序使用固定大小的数组,然后再尝试添加动态数组。如果(但仅当)您自己不知道动态数组是如何工作的,请回来问另一个问题。如果您想要动态数组,请使用@Rob向您展示的向量。@Robᵩ 我无法创建数组类型的字符串a=新字符串[4]@用户1305058。不能创建动态数组并不奇怪。这就是为什么我建议您首先使用固定大小的数组来运行程序。只有在你有了这么多完美的工作,然后用动态数组替换你的固定大小数组。
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";
}
}
#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;
}