问题描述
当我在 std :: istringstream iss(std :: move(result_string));
里面,然后它给出错误。
实际上,我得到我的字符串 result_string
里面,所以我必须执行 move
。 >
我可以这样初始化iss:
std :: istringstream iss
现在如何执行移动操作将字符串(result_string)中的缓冲区重新分配到流?意味着我可以如何执行(std :: move(s))
里面?
我的原始代码: / p>
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>
#includedictionary.h
#pragma忽略GCC诊断-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(初始化套接字%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, p_int,sizeof(int))== -1)){
printf(错误设置选项%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 bind to socket, else is listening on this port%dn,errno);
goto FINISH;
}
if(listen(hsock,10)== -1){
fprintf(stderr,Error listening%dn,errno);
goto FINISH;
}
//现在让服务器的东西
addr_size = sizeof(sockaddr_in);
while(true){
printf(waiting for a connectionn \\\
);
csock =(int *)malloc(sizeof(int));
if((* csock = accept(hsock,(sockaddr *)& sadr,& addr_size))!= -1){
printf(---------- ----------- n接收到来自%s\\\
的连接,inet_ntoa(sadr.sin_addr));
pthread_create(& thread_id,0,& SocketHandler,(void *)csock);
pthread_detach(thread_id);
} else {
fprintf(stderr,Error accepting%dn,errno);
}
}
完成:
;
}
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>最;
int max_count = 0;
typedef std :: unordered_map< std :: string,int>发生;
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} / *存储引用50个字。 * /
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 \\\
,errno);
goto FINISH;
}
printf(Received bytes%d \\\
Received string%s \\\
,bytecount,buffer);
word = strtok(buffer,);
while((NULL!= word)&&(50> i)){
ch [i] = strdup
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 \\\
,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<< \\\
;
std :: istringstream iss(std :: move(result_string));
//提取字符串直到endl,获取每个单词和单词的计数值,最大计数值
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< 最大出现次数< 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){//这里我们不能发送lenth-1。它认为确切
fprintf(stderr,发送数据错误%d \\\
,errno);
goto FINISH;
}
完成:
free(csock);
return 0;
}
在StackOverflow.com上无效。你必须在论坛中提出这个问题。
问题在这里:
std: :string s =一二二三一一二;
std :: istringstream iss(std :: move(s));
while(iss>> s)
{
int tmp = ++ s1 [s];
您正在尝试在while循环中初始化变量(int tmp)。如果你是一台计算机,这就是你看到的:做一个条件循环(正确),用这个计算初始化一个名为tmp的新变量。
一旦循环到第二个itteration,你告诉他,'初始化一个新变量tmp与这个计算',但tmp已经初始化,因此它是一个错误。
解决方案是:
std :: string s =一二二三一一二;
std :: istringstream iss(std :: move(s));
int tmp;
while(iss>> s)
{
tmp = ++ s1 [s];
When I take std::istringstream iss(std::move(result_string));
inside while then it gives error. Actually I get my string result_string
inside while, so I have to perform move
there.
I can initialize iss like this:std::istringstream iss
Now How can I perform move operation to reassign buffer from string(result_string) to stream? means how can I perform (std::move(s))
inside while?
my original code:
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);
}
}
But when entire code is in while loop, it gives error.
this is entire code if you want to see:
#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;
}
The question is valid, but not valid on StackOverflow.com. You must ask this question in a forum. See the rules.
The problem lies here:
std::string s = "one two two three one one two";
std::istringstream iss(std::move(s));
while (iss >> s)
{
int tmp = ++s1[s];
You are trying to initialize a variable (int tmp) in a while loop. If you are a computer then this is what you see: Do a while loop with a condition (correct), initialize a NEW variable called tmp with this calculation. Once the loop goes to the second itteration, you tell him, 'initialize a NEW variable called tmp with this calculation', but tmp was already initialized and therefore it's a error.
The solution is:
std::string s = "one two two three one one two";
std::istringstream iss(std::move(s));
int tmp;
while (iss >> s)
{
tmp = ++s1[s];
这篇关于当使用inside while循环时,跨越'std :: istringstream iss'的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!