|
You should be thinking in terms of containers, elements and iterators. While the ranged for loop does work for POD arrays, the dimension has to be known at compile time. For example
void showarray(int *arr)
{
for(i : arr)
std::cout << i << ' ';
std::cout << '\n';
} will not compile, since the dimension of arr is not known. On the other hand, if the compiler can deduce the size of the array, then a ranged for loop may be used with a POD array:
#include <iostream>
int main()
{
std::cout << "Enter size : ";
int n;
std::cin >> n;
int arr[n];
for(int i = 0; i < n; ++i) {
arr[i] = i;
}
for( auto x : arr) {
std::cout << x << ' ';
}
std :: cout << '\n';
} Here the compiler can deduce the dimension of the array arr and this program compiles and executes as expected. In the general case, though the ranged for loop works with containers which implement begin() and end() . That's true for STL containers, and lots of other container like objects from other libraries.
Addendum In addition to begin() and end() the container also needs to implement iterators. In the STL, you cannot use a ranged for loop with a stack , queue or priority_queue
Keep Calm and Carry On
modified 10-May-23 13:40pm.
|
|
|
|
|
A culture of entitlement.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
No idea why your question was down voted. So I up voted it.
Sometimes I can guess at skill level so it helps to frame an answer but I am unsure of your skill level. (I am using 'skill' intentionally in a very ambiguous way here.)
Do you understand what the following loop construct does?
for (int i = 1; i <= 5; ++i)
If you do not then I suggest that you experiment with that for a while. And look up different ways to structure it including replacing it with a while loop.
The structure that you asked about is just syntactic sugar that ends up implementing something similar to the above. So understanding it first helps.
|
|
|
|
|
Hello,
I'm a beginner in C++ and SFML for game creation under linux(Pop!_OS 22.04 LTS). I tested the code on the official SFML website, everything seems to work but the title bar doesn't display; it's the same with other codes. I need your help, please. Here is an example of a code I tested:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!", sf::Style::Titlebar);
sf::Event ev;
while(window.isOpen()){
while(window.pollEvent(ev)){
switch(ev.type){
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(ev.key.code == sf::Keyboard::Escape){
window.close();
break;
}
default:
break;
}
}
window.clear(sf::Color::Blue);
window.display();
}
return 0;
}
|
|
|
|
|
Your question is about SFML rather than C++. You are more likely to get an answer at SFML[^]
|
|
|
|
|
Ok. I'll post it in SFML forum.
|
|
|
|
|
When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved.
However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?
modified 23-Apr-23 14:15pm.
|
|
|
|
|
If you're responsible for managing memory you've allocated, then freeing it means it's gone; regardless of which part of your app did it. i.e. you're the one "counting the references".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
|
In case 1 you are creating a new block of dynamic memory space. When you call delete on the pointer it returns the memory space to the free pool, but the pointer still exists (until it goes out of scope, or the program ends).
In case 2 the pointer is referring to memory space that already exists, i.e. it was not created by new , so you cannot call delete on it.
|
|
|
|
|
Thank you to everyone for helping
modified 24-Apr-23 4:51am.
|
|
|
|
|
The purpose of delete is not to delete things. What?? But I'm serious. The purpose of delete is to undo new .
As an approximation that will no doubt anger several C++ experts, you can view new as a combination of malloc and invoking the constructor, and you can view delete as a combination of invoking the destructor and free . That's not exactly literally how they work, but it's close enough that you can use that to reason about when you need them. (don't use this as an excuse to delete something that was malloc 'ed)
Most of the time, if you want to get rid of something, delete is the wrong way to do it - because most things were not created by new . Trying to delete something else results at best in a failing safety check but in general corrupts the state of your application.
Most objects are destroyed implicitly, in various ways. Local variables by exiting a scope, values in containers by the container being destroyed, smart pointers do most of the rest. Explicitly using delete is occasionally necessary but it's probably better to actively avoid creating those cases. It's quite bug-prone, especially around exceptions and other non-trivial control flow.
Calin Negru wrote: later if you use “delete” on the pointer will that delete the content of the variable too? In the sense that its destructor is invoked (so you can implement it and delete the members that need to be deleted), yes. If you mean that in some other sense then, probably, no (but depending on what you mean).
Implementing classes with lots of raw-pointer members that you individually delete in the destructor is very mid-2000s. Typical advice is to delegate that to smart pointers and containers as much as possible, with the goal of following the Rule of Zero instead of one of the other rules-of-some-number, but sometimes you just have to do what you have to do.
|
|
|
|
|
Amen to this.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
Have you even looked at the documentation for QDebug?
Specifically : Quote: QDebug &QDebug::operator<<(QChar t)
Writes the character, t, to the stream and returns a reference to the stream. Normally, QDebug prints control characters and non-US-ASCII characters as their C escape sequences or their Unicode value (\u1234). To print non-printable characters without transformation, enable the noquote() functionality, but note that some QDebug backends may not be 8-bit clean and may not be able to represent it.
which leads us to:
Quote: QDebug &QDebug::noquote()
Disables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.
When quoting is disabled, these types are printed without quotation characters and without escaping of non-printable characters.
If you need quoted output, its simple enough to do it yourself, just escape the quotes (\") or use Raw literals e.g. std::string hello(R"x("Hello World")x") assigns "Hello World" , *including the quote marks" to the variable hello ,
NB. I have not tried this. I do not use QT. I don't know if this will achieve what you want. I do know how to use google, though. It's more than time enough that you did too.
Keep Calm and Carry On
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
Member 14968771 wrote: ..RTFM.. everybody knows that..." disease.
What a pity you seem immune to it.
|
|
|
|
|
Hello. I apologize in advance if I created the topic in the wrong place, because I think that my task is directly or indirectly related to WinApi. I will describe the task:
I am programming an imitation of manual work in applications for working on financial markets (Metatrader 4 and 5). I use AutoIt for this. I need to read the text of the SysListView32 items .I wrote the following code for this purpose .
#include <WinAPI.au3>
#include <Memory.au3>
#include <ListViewConstants.au3>
Local $pid= ; here i write id of needed process
Local $hwnd= ; here i write handle of SysListView32
Local $maxtext= ;here i write max number of chars
Local $hprocess=_WinAPI_OpenProcess($PROCESS_ALL_ACCESS,False,$pid)
Local $plv=_MemVirtualAllocEx($hprocess,Null,$maxtext+60,$MEM_COMMIT,$PAGE_READWRITE)
Local $rectext=DllStructCreate("char ["&$maxtext&"]")
Local $lv=DllStructCreate($tagLVITEM)
$lv.Mask=$LVIF_TEXT
$lv.Item=0
$lv.SubItem=0
$lv.Text=$plv+60
$lv.TextMax=$maxtext
_WinAPI_WriteProcessMemory($hprocess,$plv,DllStructGetPtr($lv),60,Null)
_SendMessageA($hwnd,$LVM_GETITEMA,0,$plv)
_WinAPI_ReadProcessMemory($hprocess,$lv.Text,DllStructGetPtr($rectext),$maxtext,Null)
_MemVirtualFreeEx($hprocess,$plv,0,$MEM_RELEASE)
ConsoleWrite(DllStructGetData($rectext,1))
The problem is that the above code only gets text of items if the SysListView32 has either LVS_OWNERDATA or LVS_OWNERDRAWFIXED style. I have checked above code on different applications with SysListView32 that only had style LVS_OWNERDATA and checked above code on different applications with SysListView32 that only had style LVS_OWNERDRAWFIXED. In both cases i was able to get text from SysListView32 items. But above code is not getting text of SysListView32 items with combined LVS_OWNERDATA and LVS_OWNERDRAWFIXED styles . The SysListView32 of Metatrader application have combined styles LVS_OWNERDATA+LVS_OWNERDRAWFIXED.
Well here's the question:
How to get item text from SysListView32 from another application with both LVS_OWNERDATA and LVS_OWNERDRAWFIXED styles at the same time?
I would like to get some direction of action or example code (doesn't matter on AutoIt). Thank you.
|
|
|
|
|
I am not sure that you can get the information. When a view has one of the OWNERDRAW settings it does not have a backing list of items. As the view tries to display its contents it sends a message to the parent Window which tells the application to return the text for each item. So it is the application that holds the actual data, not the ListView.
|
|
|
|
|
And how i can get actual ListView's data from application ?
|
|
|
|
|
|
There is no standard way of doing that. The data backing the owner-draw can be stored any way the application needs. It can be, literally, stored anywhere, in any format, with no public API to get at it.
Think of how you store data in your own applications. Do you expose that data in standardized ways across all of your applications? Do you expose that data in a public API? Of course not!
|
|
|
|
|
How do I change the window caption color ?
|
|
|
|
|
|