什么是最佳(从代码可读性和语义)获取指向所有std :: vector元素的指针的方式,因此它可以用作通常的数组?我以前用过:
void Func( int* )
...
std::vector<int> x(1000);
Func(&(x[0]));
Run Code Online (Sandbox Code Playgroud)
但我想知道有没有更漂亮的方法呢?x.begin()返回vector :: iterator.
我想将一个随机数与一些字母数字字符交错,例如:HELLO与随机数25635→混合H2E5L6L3O5.我知道%1d控制间距,虽然我不知道如何在随机数之间插入文本或如何实现这一点.
码:
int main(void) {
int i;
srand(time(NULL));
for (i = 1; i <= 10; i++) {
printf("%1d", 0 + (rand() % 10));
if (i % 5 == 0) {
printf("\n");
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句 - 如果我的随机数发生器不是很好,我愿意接受建议 - 谢谢
有:
std::map<const std::string,A > cache;
Run Code Online (Sandbox Code Playgroud)
你将如何插入这个容器(可以重复尝试):
cache.insert(std::make_pair(id,ps));
cache.insert(std::pair<std::string,A>(id,ps));
if(cache.find(id) == cache.end()){
cache[id] = ps;
}
Run Code Online (Sandbox Code Playgroud)
为什么??(在时间和记忆方面)
你有更好的解决方案吗?
更新:我没有使用C++ 11
Update-2: 好的,到目前为止我们意识到:
make_pair并且pair<>是类似的.
insert和[ ](有或没有if检查)都会调用copy.那么......之间的竞争是:
insert [ ](带if检查)[ ](带if检查和交换)你更喜欢哪一个?
再次感谢
我碰到这个帖子这其中试图解释volatile成员函数和const volatile成员函数.最重要的回答者说
将成员函数标记为const或volatile(或组合的const volatile)将这些限定符应用于函数中使用的this指针
以上是什么意思?如何this将方法的限定符标记为volatile或const volatile会影响this指针?
我很困惑这对方法本身意味着什么
class foo
{
void someMethod() volatile
{std::cout << "volatile method" }
void otherMethod() const volatile
{std::cout << "const volatile method"}
};
Run Code Online (Sandbox Code Playgroud) 这个程序是否应该在C++ 03中正确初始化字符串?
#include <iostream>
#include <string>
struct A
{
std::string s;
};
int main()
{
A a = { };
std::cout << a.s.size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
使用bcc32 6.70,输出是256,并检查调试器中的字符串,其内部指针似乎是垃圾地址.
我在ACE Radius库的v0.9.2中找到了以下声明:
// Types of attribute data
typedef enum AttributeFormat_e
{
E_ATTR_FORMAT_INTEGER,
E_ATTR_FORMAT_IP_ADDRESS,
E_ATTR_FORMAT_STRING,
E_ATTR_FORMAT_VENDOR_SPECIFIC,
E_ATTR_FORMAT_USER_PASSWORD,
E_ATTR_FORMAT_CHAP_PASSWORD
};
Run Code Online (Sandbox Code Playgroud)
领先typedef是完全没有意义的,不应该存在.
实际上,GCC会发出以下诊断:
/usr/include/ace-radius/RadiusAttribute.h:597:警告:此声明中忽略了'typedef'
现在,这最终是无害的,尽管在文件中是一种奇怪的半有意义的半C声明,否则只能被解析为C++(该声明被发现为a中的private成员class).
但纯粹出于好奇,我想知道这是严格遵守还是严格错误,并且无法从标准中得知.
这是typedef合法的吗?或者GCC是否宽容?
我目前有一个std::map<int,int>
像这样的价值观
Key Value
60 2
84 3
99 5
Run Code Online (Sandbox Code Playgroud)
现在我总是从一个方法中得到一个int
int a = SomeMethod();
Run Code Online (Sandbox Code Playgroud)
我想要做的是检查该数字是否在密钥的范围之间,所以如果数字是45,那么它小于密钥值60,所以我应该回来2.另一个例子是如果数字比密钥值多75 60,小于关键值84,所以我应该回来3.我目前想到的方法是我有一个数字.我会遍历地图,直到遇到一个比我想要的更大的数字.如果它是我将它从地图中删除.然后继续这样做,直到我得到一个符合我的条件的数字.我想知道是否有更好的方法来解决这个问题?
我正在尝试使用模板将类方法传递给另一个类方法,并且找不到任何关于如何做的答案(没有C++ 11,提升确定):
我将核心问题简化为:
class Numerical_Integrator : public Generic Integrator{
template <class T>
void integrate(void (T::*f)() ){
// f(); //already without calling f() i get error
}
}
class Behavior{
void toto(){};
void evolution(){
Numerical_Integrator my_integrator;
my_integrator->integrate(this->toto};
}
Run Code Online (Sandbox Code Playgroud)
我得到了错误:
error: no matching function for call to ‘Numerical_Integrator::integrate(<unresolved overloaded function type>)’this->toto);
note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (Behavior::*)()’
Run Code Online (Sandbox Code Playgroud)
谢谢.
奖金:争论怎么样?
class Numerical_Integrator{
template <class T, class Args>
double integrate(void (T::*f)(), double a, Args arg){ …Run Code Online (Sandbox Code Playgroud) 也许有很好的解决方案适用于g ++ 4.6.{3,4}?您可以登录https://godbolt.org/
#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};
template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}
int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}
Run Code Online (Sandbox Code Playgroud)
g ++ /tmp/enable_if.cpp -std = c ++ 0x
/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main() {
int x = 5;
int* y = &x;
int& z = y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于此代码,编译器给出以下错误int& z = y;:
./example.cpp: In function 'int main()':
./example.cpp:7:11: error: invalid conversion from 'int*' to 'int' [-fpermissive]
7 | int& z = y;
| ^
| |
| int*
./example.cpp:7:11: error: cannot bind rvalue '(int)y' to 'int&'
Run Code Online (Sandbox Code Playgroud)
为什么?我知道为什么会出错,而且我知道如何解决它,我很好奇为什么编译器无法隐式执行转换,对我而言,我的意图似乎微不足道,是使指针和引用引用相同的内存位置。我想念什么极端情况吗?