用一个char替换字符串中多个字符的最佳方法是什么?
string str("1 1 1");
//out: 1 1 1
Run Code Online (Sandbox Code Playgroud) 我不知道这是一个编程问题,还是通过我的操作系统配置的东西.但是我怎么能得到它所以我的控制台应用程序使用Powershell而不是cmd?我知道我可以打开Powershell并从那里运行程序,但即使这样,也会system("Get-Childitem")失败.
如果重要的话,我正在使用Visual C++.但如果它是编译器特定的东西,那么我也想知道如何使用GCC.
据我所知,只有班级可以访问数据,因此它"更安全",但不是,但我真的不明白为什么它是如此重要.也许是因为我没有让任何程序复杂到数据可能会被意外改变但是在学习课程时被告知私有化是很重要因为当我改变的时候它更"安全"程序中的数据是我明确指出的.任何人都可以提供一些例子,如果这些数据不是私有的,那么数据会被无意改变吗?
STL提供了std :: copy,但是对于具有固定大小的输出容器使用它是很棘手的,因为输出迭代器没有边界检查
所以我发明了自己的东西,如下所示
template<class InputIterator , class OutputIterator>
void safecopy( InputIterator srcStart , InputIterator srcEnd ,
OutputIterator destStart , OutputIterator destEnd )
{
while ( srcStart != srcEnd && destStart != destEnd )
{
*destStart = *srcStart;
++srcStart;
++destStart;
}
}
int main()
{
std::istream_iterator<char> begin(std::cin), end;
char buffer[3];
safecopy( begin, end, buffer, buffer + 3 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题:
我需要编写一个函数来查找数组的模式.我不擅长提出算法,但我希望其他人知道如何做到这一点.
我知道数组的大小和每个元素中的值,并且我将数组从最小到最大排序.
数组将传递给模式函数
mode = findMode(arrayPointer,sizePointer);
更新:
看完评论后我试过这个
int findMode(int *arrPTR, const int *sizePTR)
{
int most_found_element = arrPTR[0];
int most_found_element_count = 0;
int current_element = arrPTR[0];
int current_element_count = 0;
int count;
for (count = 0; count < *sizePTR; count++)
{
if(count == arrPTR[count])
current_element_count++;
else if(current_element_count > most_found_element)
{
most_found_element = current_element;
most_found_element_count = current_element_count;
}
current_element = count;
current_element_count=1;
}
return most_found_element;
}
Run Code Online (Sandbox Code Playgroud)
如果有人能把我排除在外,我仍然难以掌握这个算法.我从来没有使用过矢量,所以不要真正理解其他的例子.
我试图在我的代码中使用auto_ptr,但显然出了问题.
auto_ptr<ClassType> Class(s.Build(aFilename)); //Instantiation of the Class object
int vM = s.GetM(Class);
int vS = s.Draw(Class);
Run Code Online (Sandbox Code Playgroud)
奇怪的是,在实例化Class之后,Class对象存在,因此通过调用s.GetModelMean(Class),Class不为空.但退出函数GetM后,Class为空,因此不再可用.调用Draw函数时发生崩溃.
我按以下方式声明了这些函数:
int GetM(auto_ptr<ClassType> aM);
Run Code Online (Sandbox Code Playgroud)
似乎班级被摧毁了,但我不明白为什么......
这应该只接受字母,但它还不正确:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
std::string line;
double d;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> d == false && line != "") //false because can convert to double
{
std::cout << "its characters!" << std::endl;
break;
}
std::cout << "Error!" << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
567
Error!
Error!
678fgh
Error!
567fgh678
Error!
fhg687
its characters!
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
fhg687 应该输出错误,因为字符串中的数字.
接受的输出应仅包含字母,例如ghggjh …
方案是我将一个对象作为方法中的参数传递,我想根据方法中该对象的类型执行操作.
示例代码是:
method(Object object){
//if object== String type print string
}
Run Code Online (Sandbox Code Playgroud) 嗨,我是初学者,我想知道容器的 fill() 和 uninitialized_fill() 之间的区别。我在谷歌上进行了快速搜索,但没有得到好的答案。有人可以帮助我吗?
假设我有一个C++类,如下所示:
struct Point {
int x, y, z;
};
Run Code Online (Sandbox Code Playgroud)
我想使用Cereal将该结构序列化为JSON.所以我添加了一个这样的序列化函数:
struct Point {
int x, y, z;
template<class Archive>
void serialize(Archive& ar) {
ar(CEREAL_NVP(x),
CEREAL_NVP(y),
CEREAL_NVP(z));
}
};
Run Code Online (Sandbox Code Playgroud)
当Point是另一个对象的成员或数组的元素时,这可以正常工作.但是如果我想让Point成为整个JSON文件的主要对象,它就无法正常工作.例如,使用以下代码:
Point p { 1, 2, 3 };
cereal::JSONOutputArchive ar(std::cout);
ar(p);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
{
"value0": {
"x": 1,
"y": 2,
"z": 3
}
}
Run Code Online (Sandbox Code Playgroud)
我想删除"value0"密钥并提升对象占用整个文件,如下所示:
{
"x": 1,
"y": 2,
"z": 3
}
Run Code Online (Sandbox Code Playgroud)
我似乎唯一能做的就是基本上重新实现序列化功能,手动添加键名.
Point p {1, 2, 3};
cereal::JSONOutputArchive ar(std::cout);
ar(cereal::make_nvp("x", p.x),
cereal::make_nvp("y", p.y),
cereal::make_nvp("z", p.z));
Run Code Online (Sandbox Code Playgroud)
有没有办法利用我已经为类实现的序列化函数来实现它?