Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [C++] copiare array di chars in un vector<char>

    Salve,

    ho il seguente problema, una funzione mi ritorna:

    codice:
    // Get time
    char szTime[30]; getDateTime(szTime);
    
    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);
    }
    ed ho un vettore di caratteri:

    codice:
    std::vector<char> vChar;
    vChar.resize(30);
    vorrei copiare da szTime a vChar con l'operatore di assegnazione = ma mi da errore...come posso risolvere il problema?

    grazie!
    Alla batteria dai retta ballA

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Puoi fare direttamente così:
    codice:
    std::vector<char> vChar(&szTime[0],&szTime[30]);
    oppure se è già dichiarato:
    codice:
    std::vector<char> vChar;
    std::copy(&szTime[0],&szTime[30],std::back_inserter(vChar));
    Anche se più elegante potrebbe essere:
    codice:
    void getDateTime(std::vector<char>& szTime)
    {
    	time_t rawtime = time(NULL);
    	struct tm timeinfo;
    	gmtime_s(&timeinfo, &rawtime);
    	strftime(&szTime[0], szTime.size(), "%a, %d %b %Y %X GMT", &timeinfo);
    }
    
    std::vector<char> vChar(30);
    getDateTime(vChar);
    o in alternativa:
    codice:
    std::string getDateTime()
    {
    	time_t rawtime = time(NULL);
    	struct tm timeinfo;
    	gmtime_s(&timeinfo, &rawtime);
            std::string szTime(30,'');
    	strftime(&szTime[0], szTime.size(), "%a, %d %b %Y %X GMT", &timeinfo);
            return szTime;
    }
    
    std::string vChar = getDateTime();
    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.

  3. #3
    Codice PHP:
    #include <vector>
    #include <iostream>

    using namespace std;

    void print(vector<charvc)
    {
        for(
    vector<char>::iterator i vc.begin(); vc.end(); i++) 
            
    cout << *<< "|";
        
    cout << endl;
    }

    int main() 
    {
        
    char c[] = {'a''b''c'};
        
    int size sizeof(c)/sizeof(c[0]);
        
        
    // mediante costruttore
        
    vector<charvc(cc+size);
        print(
    vc);
        
        
    vector<charvc2;
        
    //mediante copy e back_inserter
        
    copy(cc+sizeback_inserter(vc2));
        print(
    vc2);


    edit: anticipato...

  4. #4
    ho provato il codice inserito in questo programma ma non funziona:

    codice:
    #include <windows.h>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    #include <cstdio>
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;
    
    void getDateTime(char * szTime);
    const int numbuff = 5;
    const int buflen  = 30;
    
    struct Buffer
    {
    public:
    	vector<char> vChar;
    	int bufferLength;
    	int bytesRecorded;
    	int user;
    	Buffer() : bytesRecorded(0), bufferLength(0), user(0) { };
    };
    
    int main()
    {
    	circular_buffer<Buffer> cb(numbuff);
    	circular_buffer<Buffer>::const_iterator it;
    
    	// Insert elements
    	for(int i = 0; i<10; i++)
    	{
    		// Get time
    		char szTime[30]; getDateTime(szTime);
    
    		// Init Buff
    		Buffer buff;
    		copy(&szTime[0],&szTime[30],std::back_inserter(buff.vChar));
    
    		//buff.vChar.assign(szTime, szTime+strlen(szTime));
    		buff.bufferLength  = buflen;
    		buff.bytesRecorded = buflen;
    		buff.user          = i;
    
    		printf("%d, %d, %s\n", buff.user, buff.bufferLength, szTime);
    
    		cb.push_back(buff);
    	}
    
    	// Show elements:
    	for(int i = 0; i<(int)cb.size(); i++)
    	{
    		printf("%d, %d, %s\n", cb[i].user, cb[i].bufferLength, cb[i].vChar);
    	}
    
    	system("pause");
    	return EXIT_SUCCESS;
    }
    
    // getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
    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);
    }
    nel secondo loop for, mi ritorna <null>
    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 © 2024 vBulletin Solutions, Inc. All rights reserved.