当前位置: 首页>>代码示例>>C++>>正文


C++ strvec::at方法代码示例

本文整理汇总了C++中strvec::at方法的典型用法代码示例。如果您正苦于以下问题:C++ strvec::at方法的具体用法?C++ strvec::at怎么用?C++ strvec::at使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在strvec的用法示例。


在下文中一共展示了strvec::at方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: redeclareVars

//Redeclares the vars in varlist by pushing them to outlist
//Note: Array types make this function confusing. If a variable is declared as int b[5], its entry in the varList will just be "b"
//All variables should be in the hashmap, so if we don't find "b", assume we're looking for "b["
void redeclareVars(strvec& varList, strvec& outList)
{
	for (int i = 0; i < varList.size(); i++)
	{
		std::string typeStr;
		std::string newLine;

		if (varsAndTypes.find(varList.at(i)) == varsAndTypes.end()) //if the var not there, should be array type
		{
			std::string varNameStr = varList.at(i);
			varNameStr = varNameStr.append("["); //add this to look for array types
			for (std::map<std::string, std::string>::iterator it = varsAndTypes.begin(); it != varsAndTypes.end(); it++)
			{
				if (it->first.substr(0, varNameStr.length()).compare(varNameStr) == 0) //if varnamestr is b[ and the entry in the map starts with b[, we've found it
				{
					//we want to declare it as int b[5], so we still need the type
					typeStr = it->second;
					std::string varNameStr = it->first;
					newLine = typeStr.append(" ").append(varNameStr).append(";");
				}
			}
		}
		else //if it's in there, append like always
		{
			typeStr = varsAndTypes[varList.at(i)];
			newLine = typeStr.append(" ").append(varList.at(i)).append(";");
		}

		outList.push_back(newLine);
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:34,代码来源:main.cpp

示例2: processParallelFor

//Returns true if it processes something
//Returns false if it never finds a parallel for
bool processParallelFor()
{
	bool foundPragma = false;

	for (int i = 0; i < input.size(); i++)
	{
		std::string curStr = input.at(i);
		if (curStr.substr(0, 24).compare("#pragma omp parallel for") == 0 ||
			curStr.substr(0, 15).compare("#pragma omp for") == 0)
		{
			foundPragma = true;
			int j = i + 2; //move up to pragma and pass the for loop
			if (input.at(j).substr(0,1).compare("{") == 0) //if the for is contained in brackets. weird case also where trim did not remove tab after bracket
			{
				int brackets = 1;
				//find the end of the parallel region
				while (j < input.size())
				{
					j++;

					std::string tempStr = input.at(j);
					if (tempStr.length() <= 0)
					{
						continue;
					}

					if (tempStr.at(0) == '{')
					{
						brackets++;
					}
					if (tempStr.at(0) == '}')
					{
						brackets--;
					}
					if (brackets == 0)
					{
						break;
					}
				}
			}
			else //if there are no brackets, then j is pointing to the contents of the for loop
			{
				i += 2;
				j += 2;
				input.insert(input.begin() + i, "{");
				input.insert(input.begin() + j, "}");
				i -= 2; //move i back to the pragma
			}
			//at this point, j points to the ending bracket, and i points to the pragma
			parallelForHelper(i, j);
		}
		//otherwise, move on--handle it elsewhere
	}

	return foundPragma;

}
开发者ID:dhganey,项目名称:430p2,代码行数:59,代码来源:main.cpp

示例3: processVariables

//Iterates through the whole program looking for variable declarations
//Supports ONLY int and double declarations
//NOTE: This DELETES all declarations (converts the line to ""), since they are added in appropriate locations later
void processVariables()
{
	const std::string intStr = "int";
	const std::string doubleStr = "double";

	for (int i = 0; i < input.size(); i++)
	{
		std::string curStr = input.at(i);
		std::string typeStr;
		int offset;

		//determine what is being declared
		if (curStr.substr(0, 3).compare("int") == 0) //integer declaration
		{
			if (curStr.substr(0, 8).compare("int main") == 0) //special case for this int!
			{
				continue;
			}
			typeStr = intStr;
			offset = 4;
		}
		else if (curStr.substr(0, 6).compare("double") == 0) //double declaration
		{
			typeStr = doubleStr;
			offset = 7;
		}
		else //TODO could include more primitives here
		{
			continue; //not a var declaration
		}

		//everything below here applies only to var declarations, since we continued above
		if (curStr.find(",") == std::string::npos) //if no commas
		{
			std::string varName = curStr.substr(offset, (curStr.length() - offset - 1)); //-1 to leave semicolon off
			varsAndTypes[varName] = typeStr;
			varsAndLines[varName] = i;
		}
		else //multiple vars/line
		{
			std::istringstream buf(curStr);
			std::istream_iterator<std::string> beg(buf), end;
			std::vector<std::string> tokens(beg, end);

			for (int j = 1; j < tokens.size(); j++)
			{
				std::string varName = tokens.at(j).substr(0, tokens.at(j).length() - 1); //leave off the comma or semicolon
				varsAndTypes[varName] = typeStr;
				varsAndLines[varName] = i;
			}
		}

		//now that the value is preserved in the hashmap, remove the declaration
		input.at(i) = "";
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:59,代码来源:main.cpp

示例4: processGetThreadNum

//Iterates through the whole program looking for omp_get_thread_num calls
//Replaces them with a call to the paramStruct
void processGetThreadNum()
{
	std::string replacement = "id = ((StartEnd*) paramStruct)->threadNum;";

	for (int i = 0; i < input.size(); i++)
	{
		std::string curStr = input.at(i);
		std::size_t pos = curStr.find("omp_get");
		if (pos != std::string::npos) //if we find one
		{
			//replace with a call to gettid()
			input.at(i) = replacement;
		}
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:17,代码来源:main.cpp

示例5: add

//Simple vector move function, pushes from from to to
void add(strvec& from, strvec& to)
{
	for (int i = 0; i < from.size(); i++)
	{
		to.push_back(from.at(i));
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:8,代码来源:main.cpp

示例6: printVector

//Just outputs vecRef
void printVector(strvec& vecRef)
{
	for (int i = 0; i < vecRef.size(); i++)
	{
		std::cout << vecRef.at(i) << std::endl;
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:8,代码来源:main.cpp

示例7: singleHelper

//Worker function. Start and end represent the #pragma line and the closing brace
//Drives all the work for a single
void singleHelper(int start, int end)
{
	//for single, we just want to make sure only one thread executes it
	//so, with this solution, just make it accessible only to thread 0
	//just replace the #pragma line with an if statement
	std::string newIf = "if (((StartEnd*) paramStruct)->threadNum == 0) //arbitrarily restrict it to the only guaranteed thread, 0";
	input.at(start) = newIf;
}
开发者ID:dhganey,项目名称:430p2,代码行数:10,代码来源:main.cpp

示例8: processCritical

//Returns true if it processes something
//Returns false if it never finds a critical
bool processCritical()
{
	bool foundPragma = false;

	for (int i = 0; i < input.size(); i++)
	{
		std::string curStr = input.at(i);
		if (curStr.substr(0, 20).compare("#pragma omp critical") == 0)
		{
			foundPragma = true;
			int j = i + 2; //move past the opening bracket
			int brackets = 1; //OK to assume we have an opening bracket
			//find the end of the parallel region
			while (j < input.size())
			{
				j++;

				std::string tempStr = input.at(j);
				if (tempStr.length() <= 0)
				{
					continue;
				}

				if (tempStr.at(0) == '{')
				{
					brackets++;
				}
				if (tempStr.at(0) == '}')
				{
					brackets--;
				}
				if (brackets == 0)
				{
					break;
				}
			}
			//at this point, j points to the ending bracket, and i points to the pragma
			criticalHelper(i, j);
		}
		//otherwise, move on--handle it elsewhere
	}

	return foundPragma;

}
开发者ID:dhganey,项目名称:430p2,代码行数:47,代码来源:main.cpp

示例9: declarePrivatesInMain

//Declares the varlist in the main function
void declarePrivatesInMain(strvec& varList)
{
	int mainLine = 0;
	for (mainLine; mainLine < input.size(); mainLine++)
	{
		std::string curStr = input.at(mainLine);
		if (curStr.compare("int main()") == 0)
		{
			break;
		}
	}

	mainLine += 2; //move past brackets
	for (int i = 0; i < varList.size(); i++)
	{
		input.insert(input.begin() + mainLine++, varList.at(i));
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:19,代码来源:main.cpp

示例10: insertAfterIncludes

//Pushes the vecRef into input after the include statements (at the top)
void insertAfterIncludes(strvec& vecRef)
{
	//move past any includes
	int i;
	for (i = 0; i < input.size(); i++)
	{
		if (input.at(i).length() > 0 && input.at(i).at(0) != '#')
		{
			break;
		}
	}

	//once we're here, we're past the includes and we can copy the new function
	//we need to insert this in reverse order
	for (int j = vecRef.size()-1; j >= 0; j--)
	{
		input.insert(input.begin() + i, vecRef.at(j));
	}
}
开发者ID:dhganey,项目名称:430p2,代码行数:20,代码来源:main.cpp

示例11: criticalHelper

//Worker function. Start and end represent the #pragma line and the closing brace
//Drives all the work for a critical
void criticalHelper(int start, int end)
{
	//simply lock at the beginning and the end
	input.at(start) = "pthread_mutex_lock( &theMutex );";
	input.insert(input.begin() + end + 1, "pthread_mutex_unlock (&theMutex );");
}
开发者ID:dhganey,项目名称:430p2,代码行数:8,代码来源:main.cpp

示例12: parallelForHelper

//Worker function. Start and end represent the #pragma line and the closing brace
//Drives all the work for a parallel for
void parallelForHelper(int start, int end)
{
	//grab the necessary values from the #pragma line
	std::string pragmaString = input.at(start);
	strvec privVars = getConstructVars(pragmaString, "private");
	strvec sharedVars = getConstructVars(pragmaString, "shared");
	int numThreads = getNumThreads(pragmaString);

	//create a vector for the new pthreads void* function
	std::vector<std::string> newFunction;

	std::string newFuncName;
	std::string smallNewFuncName = std::string("func").append(std::to_string(currentFunction));
	newFuncName.append("void* func").append(std::to_string(currentFunction)).append("(void* paramStruct)");
	newFunction.push_back(newFuncName);
	currentFunction++;
	newFunction.push_back("{");

	//now handle the case when a parallel for is inside a parallel. in that case, the parfor will not have priv/shared declared
	//it will instead use the priv/shared declared above
	//those are backed up in global vars.
	if (privVars.size() == 0 && sharedVars.size() == 0) //if we determine nothing is private, copy the global backup to the local strvecs
	{
		for (int i = 0; i < privVarBackup.size(); i++)
		{
			privVars.push_back(privVarBackup.at(i));
		}
		for (int i = 0; i < sharedVarBackup.size(); i++)
		{
			sharedVars.push_back(sharedVarBackup.at(i));
		}
	}

	//redeclare privates in the new function
	redeclareVars(privVars, newFunction);
	strvec privVarDeclarations;
	redeclareVars(privVars, privVarDeclarations);
	add(privVarDeclarations, totalPrivBackup);

	//globals will appear later
	redeclareVars(sharedVars, globalVars);

	//move the code to the new function
	//first modify the for loop to use the start and end from the new struct
	int numIterations = getNumIterations(input.at(start + 1));
	input.at(start + 1) = fixForLine(input.at(start + 1));

	//then copy the rest of the stuff
	for (int i = start + 1; i < end; i++)
	{
		newFunction.push_back(input.at(i));
	}
	newFunction.push_back("}");
	newFunction.push_back("}");

	//delete the #pragma section
	input.erase(input.begin() + start, input.begin() + end + 1);

	//record the offset as start since we're going to be changing the size of the vector
	int newOffset = start;

	//set up the pthreads code
	//first we need an array of pthread_t threadids with size = numthreads
	std::string tempString = std::string("pthread_t threads[").append(std::to_string(numThreads)).append("];");
	input.insert(input.begin() + newOffset++, tempString);

	//populate the threads[] array
	std::string loopVar = std::string("uniqueVar").append(std::to_string(uniqueVarNum));
	uniqueVarNum++;
	tempString = std::string("for (int ").append(loopVar).append(" = 0; ").append(loopVar).append(" < ").append(std::to_string(numThreads)).append("; ").append(loopVar).append("++)");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "{");
	tempString = std::string("threads[").append(loopVar).append("] = ").append(loopVar).append(";");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "}");

	//create the pthreads code
	int uneven = numIterations - ((numIterations / numThreads) * numThreads); //figure out how many don't go in evenly
	int basicNum = numIterations / numThreads;
	//now distribute the iterations among the pthreads
	//example: 18 iterations among 4 threads
	//break it down as follows:
	//0-3 thread 1
	//4-7 thread 2
	//8-11 thread 3
	//12-17 thread 4
	//or 12-14 thread 4

	int startIteration = 0;
	int endIteration = basicNum - 1;
	for (int i = 0; i < numThreads; i++)
	{
		tempString = std::string("StartEnd paramStruct").append(std::to_string(i)).append(";");
		input.insert(input.begin() + newOffset++, tempString);

		tempString = std::string("paramStruct").append(std::to_string(i)).append(".start = ").append(std::to_string(startIteration)).append(";");
		input.insert(input.begin() + newOffset++, tempString);
		startIteration += basicNum;
//.........这里部分代码省略.........
开发者ID:dhganey,项目名称:430p2,代码行数:101,代码来源:main.cpp

示例13: parallelHelper

//Worker function. Start and end represent the #pragma line and the closing brace
//Drives all the work for a parallel
void parallelHelper(int start, int end)
{
	//grab the necessary values from the #pragma line
	std::string pragmaString = input.at(start);
	strvec privVars = getConstructVars(pragmaString, "private");
	strvec sharedVars = getConstructVars(pragmaString, "shared");
	int numThreads = getNumThreads(pragmaString);

	//save the values in a backup for redeclaration in an inner function
	privVarBackup.clear();
	for (int i = 0; i < privVars.size(); i++)
	{
		privVarBackup.push_back(privVars.at(i));
	}
	sharedVarBackup.clear();
	for (int i = 0; i < sharedVars.size(); i++)
	{
		sharedVarBackup.push_back(sharedVars.at(i));
	}

	//create a vector for the new pthreads void* function
	std::vector<std::string> newFunction;

	//add to the new function
	std::string newFuncName;
	std::string smallNewFuncName = std::string("func").append(std::to_string(currentFunction));
	newFuncName.append("void* func").append(std::to_string(currentFunction)).append("(void* paramStruct)");
	newFunction.push_back(newFuncName);
	currentFunction++;
	newFunction.push_back("{");

	//redeclare variables in the new function
	redeclareVars(privVars, newFunction);
	strvec privVarDeclarations;
	redeclareVars(privVars, privVarDeclarations);
	add(privVarDeclarations, totalPrivBackup);

	//stick shared vars in the global vector for declaration later
	redeclareVars(sharedVars, globalVars);

	//copy the function code as-is
	for (int i = start + 2; i < end; i++) //move start up to pass first bracket, < end to not include last bracket.
	{
		newFunction.push_back(input.at(i));
	}
	newFunction.push_back("}");

	//delete the #pragma section
	input.erase(input.begin() + start, input.begin() + end + 1);

	//record the offset as start since we're going to be changing the size of the vector
	int newOffset = start;

	//set up the pthreads code
	//first we need an array of pthread_t threadids with size = numthreads
	std::string tempString = std::string("pthread_t threads[").append(std::to_string(numThreads)).append("];"); //TODO do we need to populate this?
	input.insert(input.begin() + newOffset++, tempString);

	//populate the threads[] array
	std::string loopVar = std::string("uniqueVar").append(std::to_string(uniqueVarNum));
	uniqueVarNum++;
	tempString = std::string("for (int ").append(loopVar).append(" = 0; ").append(loopVar).append(" < ").append(std::to_string(numThreads)).append("; ").append(loopVar).append("++)");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "{");
	tempString = std::string("threads[").append(loopVar).append("] = ").append(loopVar).append(";");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "}");

	//now create the pthreads
	for (int i = 0; i < numThreads; i++)
	{
		tempString = std::string("StartEnd paramStruct").append(std::to_string(i)).append(";");
		input.insert(input.begin() + newOffset++, tempString);

		tempString = std::string("paramStruct").append(std::to_string(i)).append(".threadNum = ").append(std::to_string(i)).append(";");
		input.insert(input.begin() + newOffset++, tempString);

		tempString = std::string("pthread_create(&threads[").append(std::to_string(i)).append("], NULL, ").append(smallNewFuncName).append(", (void*) &paramStruct").append(std::to_string(i)).append(");");
		input.insert(input.begin() + newOffset++, tempString);
	}

	//now set up a for loop to join the pthreads
	loopVar = std::string("uniqueVar").append(std::to_string(uniqueVarNum));
	uniqueVarNum++;
	tempString = std::string("for (int ").append(loopVar).append(" = 0; ").append(loopVar).append(" < ").append(std::to_string(numThreads)).append("; ").append(loopVar).append("++)");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "{");
	tempString = std::string("pthread_join(threads[").append(loopVar).append("], NULL);");
	input.insert(input.begin() + newOffset++, tempString);
	input.insert(input.begin() + newOffset++, "}");

	//insert the new stuff, in reverse order!!
	insertAfterIncludes(newFunction); //first, new function
}
开发者ID:dhganey,项目名称:430p2,代码行数:96,代码来源:main.cpp


注:本文中的strvec::at方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。