Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1

    [C++] struttura di struttura

    Salve,

    sono alle prese con il seguente codice:

    codice:
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			std::copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    
    struct circular
    {
    	boost::circular_buffer<buffer> cb[numbuff];
    };
    
    int main()
    {
    	char szTime[30]; getDateTime(szTime);
    
    	circular c;
    
    	c.cb[0].push_back(buffer((unsigned char*)szTime, 30, 1));
    	Sleep(1000);
    
    	c.cb[1].push_back(buffer((unsigned char*)szTime, 30, 2));
    	Sleep(1000);
    
    	c.cb[2].push_back(buffer((unsigned char*)szTime, 30, 3));
    	Sleep(1000);
    }
    quindi per accedere in scrittura alla struttura buffer devo attraversare la struttura circular. Secondo voi posso aggiungere nella struttura circual un costruttore che mi eviti di fare:

    codice:
    c.cb[0].push_back(buffer((unsigned char*)szTime, 30, 1));
    così che posso fare magari: Circular c((unsigned char*)szTime, 30, 1)) ???

    forse anchio mi sono perso nel tragitto della struttura...

    grazie
    Alla batteria dai retta ballA

  2. #2
    credo che questo vada bene:

    codice:
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    
    struct circular
    {
    	circular_buffer<buffer> cb[numbuff];
    
    	circular(const unsigned char * data_, const int bytesRecorded_, const int user_)
    	{
    		cb->push_back(buffer(data_, bytesRecorded_, user_));
    	}
    };
    in teoria all'interno del costruttore cb->push_back spinge un nuovo buffer (Last In First Out) dove l'anello è composto da numbuff buffers

    Ora posso inizializzare il buffer così:

    codice:
    	// Prepare buffers:
    	for(int i = 0; i<numbuff; i++)
    	{
    		circular c(NULL, 0, 0); // default
    	}
    rimane sempre il problema che non posso accedere agli elementi di buffer...
    Alla batteria dai retta ballA

  3. #3
    cmq il problema rimane...tornando alla struttura precedente:

    codice:
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;
    
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    
    struct circular
    {
    	circular_buffer<buffer> cb[numbuff];
    };
    ora inizio a spingere i buffers (buffer circolare)

    codice:
    map<int, circular> users;
    
    	char szTime[30]; getDateTime(szTime);
    
    	circular c;
    	c.cb->push_back(buffer((unsigned char*)szTime,30,1)); // primo
    	c.cb->push_back(buffer((unsigned char*)szTime,30,1)); // secondo
    	c.cb->push_back(buffer((unsigned char*)szTime,30,1)); // terzo
    	c.cb->push_back(buffer((unsigned char*)szTime,30,1)); // sovrascrive primo
    
    	users.insert(make_pair(1000, c));
    fin quì tutto bene...ma dal consumer come faccio a leggere il dato??
    Alla batteria dai retta ballA

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Hai provato con:
    c.cb->at(indice) /* . o -> */ nomedato;

    ?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    beh diciamo che provo a leggere il dato dalla mappa, quindi:

    codice:
    cout << users[1000].cb->at(0).user << endl;
    ma mi va in errore la libreria boost
    Alla batteria dai retta ballA

  6. #6
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    E cosa dice la documentazione della libreria?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  7. #7
    non dice molto...ma il problema è solo mi...se suo la libreria in questo modo:

    codice:
    circular_buffer<buffer> cb(numbuff);
    
    /*
    	// Insert elements
    	// faccio a posta a scrivere 5 volte in un buffer circolare
    	// da 3 celle
    
    	cout << endl << "Push elements:" << endl;
    	for(int i = 0; i < 5; i++)
    	{
    		char szTime[30]; getDateTime(szTime);
    
    		cb.push_back( buffer((unsigned char*)szTime, 30, i) );
    
    		cout << szTime << endl;
    
    		Sleep(1000);
    	}
    
    	// Show elements
    	cout << endl << "Show elements:" << endl;
    	for(int i = 0; i<(int)cb.size(); i++)
    	{
    		cout << cb[i].user;
    		cout << " -> " << &cb[i].data[0];
    		cout << " " << sizeof(cb[i].data) << endl;
    	}
    
    	// Last element
    	cout << endl << "Last elements:" << endl;
    	cout << &cb[numbuff-1].data[0] << endl;
    */
    il problema è quando circular_buffer<buffer> cb(numbuff) lo metto dentro una struttura a sua volta...forse non è permesso di avere un arrau di buffer circolari in una struttura?
    Alla batteria dai retta ballA

  8. #8
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    E che errore ti da esattamente?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  9. #9
    non importa, forse ho risolto così:

    codice:
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    
    struct circular
    {
    	circular_buffer<buffer> cb;
    };
    
    int main()
    {
    	circular c;
    	c.cb.set_capacity(numbuff);
    
    	// Prepare buffer
    	for(int i = 0; i<numbuff; i++)
    	{
    		c.cb.push_back(buffer(NULL,0,0));
    	}
    
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,4));
    
    	// Get last element
    	cout << c.cb.at(numbuff-1).user << endl;
    
    	// get first element
    	cout << c.cb.at(0).user << endl;
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    Alla batteria dai retta ballA

  10. #10
    e così anche il discorso della mappa!!!

    codice:
    #include <windows.h>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;
    
    void getDateTime(char * szTime);
    const int numbuff = 3;
    const int buflen  = 30;
    
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    
    struct circular
    {
    	circular_buffer<buffer> cb;
    };
    
    int main()
    {
    	circular c;
    	c.cb.set_capacity(numbuff);
    
    	// Prepare buffer
    	for(int i = 0; i<numbuff; i++)
    	{
    		c.cb.push_back(buffer(NULL,0,0));
    	}
    
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
    	c.cb.push_back(buffer((unsigned char*)"hellow world!", 15,4));
    
    	// Get last element
    	cout << c.cb.at(numbuff-1).user << endl;
    
    	// get first element
    	cout << c.cb.at(0).user << endl;
    
    	/*
    	 *
    	 * Prova std::map<int, circular> users;
    	 *
    	 */
    
    	map<int, circular> users;
    
    	circular k;
    	k.cb.set_capacity(numbuff);
    
    	// Prepare buffer
    	for(int i = 0; i<numbuff; i++)
    	{
    		k.cb.push_back(buffer(NULL,0,0));
    	}
    
    	// Add buffer
    	k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
    	k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
    	k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
    	k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,4));
    
    	// Push in the map
    	users.insert(make_pair(1000, k));
    
    	// Show map: element 1000
    	cout << users[1000].cb.at(0).user << endl;
    	cout << users[1000].cb.at(numbuff-1).user << endl;
    
    
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    void getDateTime(char * szTime)
    {
    	time_t rawtime = time(NULL);
    	struct tm timeinfo;
    	gmtime_s(&timeinfo, &rawtime);
    	strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
    }
    e mi ci sono volute solo 15 oreeeeeeeeeeee
    Alla batteria dai retta ballA

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.