我有一个文本文件,其中包含许多单词;.它看起来像这样:
eye;hand;mouth;arms;book
three;head;legs;home
Run Code Online (Sandbox Code Playgroud)
我想通过这个文件并搜索符号;并修改文件,以便每个单词都换行换行符.
我应该先用字符串读取文本文件,
string path = @"c:\temp\MyTest.txt";
string readText = File.ReadAllText(path);
Run Code Online (Sandbox Code Playgroud)
然后检查:
if readText.contains(";");
Run Code Online (Sandbox Code Playgroud)
但我不知道接下来该做什么
考虑一下,我已经初始化了unique_ptr这样的:
unique_ptr<uint_8[]> pixels{nullptr};
Run Code Online (Sandbox Code Playgroud)
之后,我决定分配一个新数组:
pixels = new uint_8[10];
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不允许分配一个大小为10*8的新数组.我知道,我可以简单地分配,std::make_unique<uint_8[]>(10)但我只是想了解智能指针.
所以基本上,问题是:
nullptr转换为新数组?nullptrC++ 11中是否有某些特定类型?给出以下示例,我希望解析器将std识别为函数:
#include <algorithm>
namespace test
{
class foo{};
void std(foo f);
}
int main()
{
std(test::foo());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,使用GCC 4.8.4会导致错误 - "错误:意外命名空间名称'std':预期表达式."
使用clang 5.0,我得到"错误:意外命名空间名称'std':期望表达式"
这是预期的吗?我无法想象解析器在这里没有足够的上下文来区分表达式和命名空间?
编辑:示例使用应该调用ADL的更复杂的类型.在我的实际用例中,代码是通用的,我需要ADL.
我正试图移动unique_ptr到WriteAsync方法.这按预期工作.我现在遇到的问题是将唯一指针的所有权移入strand.postlambda,然后再将其移动到QueueMessage.
QueueMessage需要一个std::unique_ptr<std::vector<char>>.
在这种情况下,对我来说简单的方法就是使用a shared_ptr.我想知道是否有办法在不使用a的情况下完成这项工作shared_ptr.
// Caller
static void DoWork( char const* p, int len )
{
client.WriteAsync( std::make_unique<std::vector<char>>( p, p + len ) );
}
// Callee
void TcpClient::WriteAsync( std::unique_ptr<std::vector<char>> buffer )
{
_strand.post( [ this, buffer = std::move( buffer ) ]( )
{
// Error on this line.
QueueMessage( std::move( buffer ) );
} );
}
void TcpClient::QueueMessage( std::unique_ptr<std::vector<char>> buffer )
{
// Do stuff …Run Code Online (Sandbox Code Playgroud) 如何在WP7中的Listpicker中添加项目?使用XAML.以简单的方式.
嗨,大家好我知道的NaN(让我说我知道的缩写,代表非数字)是,但我不明白为什么C++返回它 - 以下是数学常数e的逼近 - 当使用调试器的功能评估很好,当它写入控制台时,它返回NaN
感谢您的任何反馈
double Factorial(int k)
{
if(k == 0)
return 1;
int value = 1;
for(int i = k; i > 0; i--)
value *= k;
return value;
}
double e(int p)
{
double value = 0.0;
for(int i = 0; i < p; i++)
{
value += 1/Factorial(i);
}
}
Run Code Online (Sandbox Code Playgroud) 有没有搞错 ?(此后报价后以粗体显示的真实问题)
§20.7.2.2.1
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
23要求:Y*应可转换为T*.24效果:构造一个与r共享所有权的shared_ptr对象,并存储r中存储的指针的副本.
25后置条件:use_count() == r.use_count().
26投掷:bad_weak_ptr何时r.expired().
27异常安全:如果抛出异常,则构造函数无效.
这不是助推行为.从过期的弱构造的共享给出一个空的共享.你可以在布尔上下文中测试它.
为什么委员会选择了例外的方式?例如,谷歌C++指南完全取消了异常使用.具有此类指导的项目,甚至在构建时禁用异常(在授权禁用的编译器上)将如何禁用?
最后,如果这可能经常发生(开发人员依赖过期的指针检测作为正常的程序流),那么它(对于实时程序)是不是很危险?我记得有一篇文章提到了实现异常的两种可能的策略,一种是减慢一切,但不是真的在异常发生时,另一种只有在异常发生时才会缓慢,但不会影响其余部分.我想这在某种程度上仍然必须成立.
我有这个非常简单的课程
class myclass {
public:
int id;
double x, y, z;
myclass() = default; // If I omit this line I get an error
myclass(int ID, double X, double Y, double Z): id(ID), x(X), y(Y), z(Z) {};
};
Run Code Online (Sandbox Code Playgroud)
如果我省略了该行的行myclass() = default;,然后尝试创建一个对象
#include <vector>
using namespace std;
int main() {
int ID = 0;
double X = 1.0, Y = 2.0, Z = 3.0;
vector<myclass> a_vector(10);
myclass an_object(ID,X,Y,Z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误no matching function for call to ‘myclass::myclass() …
我有一个结构集.
struct Neighbour
{
int ID;
int Left;
int Right;
}
set<NodeNeighbour>NextDoor;
Run Code Online (Sandbox Code Playgroud)
如何在此集中找到ID =='要搜索的数字'?
使用straigth前向集,可以使用set.find()简单地找到一个项目.搜索一组结构有什么类似的东西吗?
Thx提前
我前几天正在使用xcode,正在开发基于文本的对话游戏.
#include <iostream>
int main()
{
std::cout<<"Conversation simulator requires you to type in all caps for all of your replies"<<std::endl;
std::cout<<"Type your first and last name!"<<std::endl;
std::string fname = " ";
std::string lname = " ";
bool samename = false;
std::cin>> fname >> lname ;
if (fname == "MUSCLE" and lname == "MAN")
std::cout<< "Wow, we have the same first and last name!"<<std::endl;
samename = true;
else
std::cout<< fname << " "<< lname << "is a very nice name"<<std::endl;
return …Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×4
c# ×1
c++14 ×1
exception ×1
if-statement ×1
math ×1
nan ×1
shared-ptr ×1
text-files ×1
unique-ptr ×1
weak-ptr ×1
xcode ×1