当使用while循环时,跨越'std :: istringstream iss'的初始化

时间:2013-08-24 10:49:40

标签: c++

当我将std::istringstream iss(std::move(result_string));置于内部时,它会出错。 实际上我在里面得到了我的字符串result_string,所以我必须在那里执行move

我可以像这样初始化iss: std::istringstream iss

现在如何执行移动操作以将缓冲区从字符串(result_string)重新分配给流?意味着如何在内部执行(std::move(s))

我的原始代码:

std::string s = "one two two three one one two";
std::istringstream iss(std::move(s));
        while (iss >> s)
    {
        int tmp = ++s1[s];
        if (tmp == max_count)
        {
            most.push_back(s);
        }
        else if (tmp > max_count)
        {
            max_count = tmp;
            most.clear();
            most.push_back(s);
        }
    }

但是当整个代码在while循环中时,它会给出错误。

如果你想看到这是完整的代码:

#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <algorithm>
#include <unordered_map>
#include <pthread.h>                        
#include <iostream>
#include <sstream>
#include <cstdio>
#include "dictionary.h"
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
void *SocketHandler(void *);

int main(int argv, char **argc)
{
    int host_port = 1103;
    char buf[20];
    int k;
    struct sockaddr_in my_addr;
    int hsock;
    int *p_int;
    int err;
    socklen_t addr_size = 0;
    int *csock;
    sockaddr_in sadr;
    pthread_t thread_id = 0;

    hsock = socket(AF_INET, SOCK_STREAM, 0);
    if (hsock == -1) {
    printf("Error initializing socket %dn", errno);
    goto FINISH;
    }

    p_int = (int *) malloc(sizeof(int));
    *p_int = 1;

    if ((setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char *) p_int, sizeof(int)) == -1) || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char *) p_int, sizeof(int)) == -1)) {
    printf("Error setting options %dn", errno);
    free(p_int);
    goto FINISH;
        }
    free(p_int);

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(host_port);

    memset(&(my_addr.sin_zero), 0, 8);
    my_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(hsock, (sockaddr *) & my_addr, sizeof(my_addr)) == -1) {
    fprintf(stderr, "Error binding to socket, make sure nothing else is listening on this port %dn", errno);
    goto FINISH;
    }
    if (listen(hsock, 10) == -1) {
    fprintf(stderr, "Error listening %dn", errno);
    goto FINISH;
    }
    //Now lets do the server stuff

    addr_size = sizeof(sockaddr_in);

    while (true) {
    printf("waiting for a connectionn\n");
    csock = (int *) malloc(sizeof(int));
    if ((*csock = accept(hsock, (sockaddr *) & sadr, &addr_size)) != -1) {
        printf("---------------------nReceived connection from %s\n", inet_ntoa(sadr.sin_addr));
        pthread_create(&thread_id, 0, &SocketHandler, (void *) csock);
        pthread_detach(thread_id);
    } else {
        fprintf(stderr, "Error accepting %dn", errno);
    }
    }

  FINISH:
    ;
}
void *SocketHandler(void *lp)
{
    int ar[2];
    int result=0;
    int *csock = (int *) lp;
    char buf[20];
    int k;
    char *skp;
    char *str;
    char *str2;

    std::string ss;
    std::ostringstream bfr; 
    std::string result_string;
    std::vector<std::string> most;
    int max_count = 0;
    typedef std::unordered_map<std::string,int> occurrences;
    occurrences s1;

    int pcount = 0, ncount = 0;
    char buffer[1024];
    int buffer_len = 1024;
    int bytecount;

    int i = 0,t=0,q=0;
    int j = 0;
    char *ch[50] = { 0 };   /* stores references to 50 words. */
    char *ch2[50] = { 0 };  
    char *excluded_string[50] = { 0 };

        char *word = strtok(buffer, " ");
        char *word2 = strtok(buffer, " ");

        char *portstring1=(char *)malloc(sizeof(buffer));
        char *portstring2=(char *)malloc(sizeof(buffer));

    memset(buffer, 0, buffer_len);
    if ((bytecount = recv(*csock, buffer, buffer_len, 0)) == -1) {
    fprintf(stderr, "Error receiving data %d \n", errno);
    goto FINISH;
    }
    printf("Received bytes %d \nReceived string  %s \n ", bytecount, buffer);
    word = strtok(buffer, " ");

    while ((NULL != word) && (50 > i)) {
    ch[i] = strdup(word);
    excluded_string[j]=strdup(word);
    word = strtok(NULL, " ");
    skp = BoyerMoore_skip(ch[i], strlen(ch[i]) );
    if(skp != NULL)
    {
        i++;
        continue;
    }
    printf("exclueded : %s  and %s size %d \n",excluded_string[j],ch[i],sizeof(excluded_string));
    bfr << excluded_string[j] << " ";
    result_string = bfr.str();
    j++;    
    //  std::cout << "string is :" << r1;
    str = BoyerMoore_positive(ch[i], strlen(ch[i]) );
    str2= BoyerMoore_negative(ch[i], strlen(ch[i]) );
    if (str == NULL)
        t++;
    else {
        pcount += 1;
    }

    if(str2== NULL)
        q++;
    else 
        ncount += 1;

    i++;
    if(str==NULL && str==NULL and skp !=NULL)
    {
        pcount=0;
        ncount=0;
    }
    }
        std::cout << "string is :" << result_string << "\n";
    std::istringstream iss(std::move(result_string));

        //extract the string till endl, get count values for each word and word with max count value
        while (iss >> result_string)
        {
            int tmp = ++s1[result_string];
            if (tmp == max_count)
            {
                most.push_back(result_string);
            }
            else if (tmp > max_count)
            {
                max_count = tmp;
                most.clear();
                most.push_back(result_string);
            }
        }

        std::cout << std::endl << "Maximum Occurrences" << std::endl;
    for (std::vector<std::string>::const_iterator it = most.cbegin(); it != most.cend(); ++it)
            std::cout << *it << std::endl;


    ar[0]=pcount;
    ar[1]=ncount;

    if ((bytecount = send(*csock, (char *)ar, 2 *sizeof(int), 0)) == -1) { // Here we cant send lenth-1. It consider exact
    fprintf(stderr, "Error sending data %d\n", errno);
    goto FINISH;
    }

  FINISH:
    free(csock);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

该问题有效,但在StackOverflow.com上无效。你必须在论坛中提出这个问题。请参阅规则。

问题在于:

std::string s = "one two two three one one two";
std::istringstream iss(std::move(s));
    while (iss >> s)
    {
        int tmp = ++s1[s];

您正尝试在while循环中初始化变量(int tmp)。如果您是计算机,那么这就是您所看到的:使用条件执行while循环(正确),使用此计算初始化名为tmp的NEW变量。 一旦循环进入第二次迭代,你告诉他'用这个计算初始化一个叫做tmp的新变量',但是tmp已经初始化了,因此它是一个错误。

解决方案是:

std::string s = "one two two three one one two";
std::istringstream iss(std::move(s));
    int tmp;
    while (iss >> s)
    {
        tmp = ++s1[s];