我目前正在尝试理解 C++ 中的变量参数列表。我有一个来自 Java 的特定示例,我正在尝试开始工作:
class FindMax {
public static void main(String... args) {
System.out.println(findMax(3, 5, 7));
}
public static int findMax(int... n) {
int largest = n[0];
for (int i = 1; i < n.length; i++)
largest = (largest > n[i]) ? largest : n[i];
return largest;
}
}
Run Code Online (Sandbox Code Playgroud)
幸运的是,我在cplusplus.com上找到了这个例子。我尝试运行此代码(在某种程度上甚至对我有意义):
#include <stdio.h>
#include <stdarg.h>
int FindMax (int n, ...) {
int i,val,largest;
va_list vl;
va_start(vl,n);
largest=va_arg(vl,int);
for (i=1;i<n;i++) {
val=va_arg(vl,int);
largest=(largest>val)?largest:val;
}
va_end(vl);
return largest;
}
int …Run Code Online (Sandbox Code Playgroud) 我试图将时间格式化为 hh::mm::ss 然后将其放入宽字符串流中。代码如下。
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::chrono::steady_clock::duration time_elapsed = end - start;
std::chrono::hh_mm_ss formatted {std::chrono::duration_cast<std::chrono::milliseconds> (time_elapsed)};
Run Code Online (Sandbox Code Playgroud)
start位于类的构造函数中。使用<<流运算符不起作用,我没有看到任何方法将此类型转换为字符串。
我的问题是如何转换formatted为字符串(c 风格、wstring 或普通字符串)?
有没有办法从使用 fgets 读取的字符串末尾删除 \N 字符,而不使用字符串处理函数(strlen、strtok、strcspn 等)?我试过了,但没用
char* remove_newline_char(char *str){
for(int i = 0; i < str[i]; i++){
if(str[i] == '\n'){
str[i] = '\0';
}
}
return str;
}
Run Code Online (Sandbox Code Playgroud) 给定一个自定义向量并使用 std::allocator 进行分配,在 C++17 及更高版本下,我们仍然需要使用alignas创建内部过度对齐类型OT,然后为OT分配,并在取消引用指针之前将reinterpret_cast OT* 转换为T*在迭代器运算符*上,为了支持类型的过度对齐?
或者 std::allocator 是否正确对齐,并且取消引用 T* 是否可以完美地返回对元素的正确过度对齐引用?
感谢您的任何澄清。
我的任务是按长度递增的顺序对字符串中的单词进行排序,对于相同长度的单词,我必须按照给定的顺序对它们进行排序。例如:“成为或不成为”将变成“成为或不成为”。我首先制作字符串中所有单词的向量“v”,然后尝试使用 C++ 的 sort() 函数中的用户定义函数对向量进行排序。这是我的代码:
#include <bits/stdc++.h>
using namespace std;
static bool comparelength(string first,string second){//function to compare length
return first.size()<second.size();
}
int main() {
string text="Jlhvvd wfwnphmxoa qcuucx qsvqskq cqwfypww dyphntfz hkbwx xmwohi qvzegb ubogo sbdfmnyeim tuqppyipb llwzeug hrsaebveez aszqnvruhr xqpqd ipwbapd mlghuuwvec xpefyglstj dkvhhgecd kry";
vector<string> v;
string cur="";
text+=" ";
for(int i=0;i<text.size();i++){
if(text[i]==32){//if space is encountered then the word is inserted in the vector
v.push_back(cur);
cur="";
}
else{
cur+=text[i];//if not space then text[i] is added to the current …Run Code Online (Sandbox Code Playgroud) 我刚开始学习智能指针,遇到了一个似乎与理论相矛盾的情况。例如,当我们使用 shared_ptr 并且如果调用复制语义时,对象共享所有权并且引用计数为 2。这是可以理解的。例如代码。
class Test {
public:
Test(){ cout << "Const" << "\n"; }
void Disp(){
cout << "Class Test()\n";
}
~Test(){ cout << "Dest" << "\n"; }
};
int main()
{
Test *p = new Test();
shared_ptr<Test> p1(p);
shared_ptr<Test> p2(p1); // = make_shared<Test>(*p1);
p1->Disp();
p2->Disp();
}
Run Code Online (Sandbox Code Playgroud)
输出很好,因为:
Const
Class Test()
Class Test()
Dest
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试将语义移动为
int main()
{
Test *p = new Test();
shared_ptr<Test> p1(p);
shared_ptr<Test> p2 = make_shared<Test>(*p1);
p1->Disp();
p2->Disp();
}
Run Code Online (Sandbox Code Playgroud)
那么 p1 应该已经失去了所有权。因此 p1->Disp() 应该是有效的。因为 make_shared 将转移所有权并重置 …
假设有两个线程,其中一个线程具有更高的优先级,并且它们在同一核心(单核)上运行,我只想同时只工作一个线程。(也许你说这不是线程范例,但实际上我只是做了我的这里问题最小)
T1 ~~~e1~e2~e3~e4~...~~~~~~~eK~~~~~~~~...~~~ eN~~~~~ ///(e1...eN)packed as a task.
| |
T2 ~~~~~~~~~~~~pause~~~~~~continue~~~~~~~~~~~~~~~~~~ ///(pause & continue)is just title time for T1 that T2 is operating (T2 has more priority).
Run Code Online (Sandbox Code Playgroud)
**~**是 time ,并且**e**是在那里求值的表达式。整个 e1、e2、... 是一个函数,即 api 调用函数(任务),所以我只想在那里暂停 T1(~pause~)并运行我的 T2 直到完成,完成后继续 T1。
注意:我无法更换
**e**工作(职能)。
我知道的?
创建条件变量(CV)并在完成时T2通知CV唤醒,但这不是我的成就,因为我想让T1立即在e4 (暂停时间)中精确暂停并继续T2直到完成(或我的继续时间)。
我的知识与: https: //en.cppreference.com/w/cpp/thread/condition_variable#Example 我们是否有立即暂停的 thread::方法(强制上下文切换)?(我不是说屈服!)
我尝试编写,const char TabArray[32]{'\t'};因为我想将最多 32 个字节的选项卡传递到函数中(不需要 null),并且编写\t32 次= "\t\t会使我的源代码看起来很丑
如果我想要它 const 我还有其他选择吗?
我有一个for循环遍历一个对象向量.如果一个对象不满足条件,我想通过循环重复相同的对象,直到它满足条件.
int TrainDog(const vector<Dog> &dogs, const Cat big_cat) {
for (auto const dog : dogs) {
dog->Sit(); // tell the dog to sit
if (!dog->IsBarking()) // if dog isn't barking
dog->Eat(raw_burger); // then reward dog
else { // else
dog->PlayWith(big_cat); // punish dog
??? // and train again ???
big_cat++; // with bigger cat
}
}
}
Run Code Online (Sandbox Code Playgroud)
我宁愿保留这个干净的迭代器,而不是使用传统的索引变量语法.
我只需要从一个浮动对象到另一个浮动对象的哈希图。应该很简单,不是吗?编译器只是不接受它:
宣言:
unordered_map<float, float> m_mffPhotoPeakMap;
Run Code Online (Sandbox Code Playgroud)
使用:
float CProductSpecs::AddToMap(float fEnergy, float returnedValue) const
{
auto pair = make_pair(fEnergy, returnedValue);
m_mffPhotoPeakMap.insert(pair); // Error! (First attempt)
m_mffPhotoPeakMap[fEnergy] = returnedValue; // Error! (Second attempt)
return returnedValue;
}
Run Code Online (Sandbox Code Playgroud)
错误消息(第一次尝试):
Severity Code Description Project File Line Suppression State
Error C2663 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::insert': 6 overloads have no legal conversion for 'this' pointer
Run Code Online (Sandbox Code Playgroud)
错误消息(第二次尝试):
Severity Code Description Project File Line Suppression State
Error C2678 binary '[': no operator found which takes a left-hand operand of type 'const std::unordered_map<float,float,std::hash<float>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' …Run Code Online (Sandbox Code Playgroud) 假设我声明了一个指向int的指针
int *a= new int();
int *b= new int();
Run Code Online (Sandbox Code Playgroud)
我希望它将其地址存储在某个数组中,但数组不会是这样的指针
int *arr[] = {a, b};
Run Code Online (Sandbox Code Playgroud)
它有可能吗?
因为数组名称包含地址,如果我们声明一个数组指针,那么它们将再次包含地址相同的东西,
那么如何在常规数组中存储指针而不是指针数组.喜欢
int arr[] = {a,b} ; //not like int arr[] = {*a, *b};
Run Code Online (Sandbox Code Playgroud)如果不是这样,为什么?